Hello Sir,
The Project i create will capture image from a document scanner device...I am using ASP.Net Core MVC
I'd just copied the API Controller for the sample...
>>> below, if the Map controller route like this, it works fine if this Controller is the startup page.
app.UseEndpoints(endpoints =>{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Default}/{action=Index}");
});
(this is my Startup.cs)
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.UseSession();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Default}/{action=Index}");
});
}
>>> below, if the startup Map controller route like this, it will generate an error to the production server..
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "Home",
pattern: "{controller=Home}/{action=Index}");
});
>>> below is the API Controller located in CONTROLLER on the Folder "API"
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.IO;
namespace FILMSv1.Controllers
{
[Produces("application/json")]
[Route("api/ImageUploadApi")]
public class ImageUploadApiController : Controller
{
public ImageUploadApiController(IWebHostEnvironment enviroment)
{
_enviroment = enviroment;
}
private IWebHostEnvironment _enviroment;
/// <summary>
/// Gets an information about the web hosting environment an application is running in.
/// </summary>
public IWebHostEnvironment Enviroment
{
get { return _enviroment; }
}
[HttpPost]
public IActionResult Post()
{
IFormCollection form = Request.Form;
try
{
if (form != null)
{
string imageFileName = form["fileName"];
if (imageFileName == null)
throw new ArgumentException("Request does not contain image file name.");
string imageFileAsBase64String = form["imageFileAsBase64String"];
if (imageFileAsBase64String == null)
throw new ArgumentException("Request does not contain image file data.");
byte[] imageFileBytes = Convert.FromBase64String(imageFileAsBase64String);
// get the image file extension
string fileExtension = Path.GetExtension(imageFileName).ToLower();
if (fileExtension != ".bmp" && fileExtension != ".jpg" && fileExtension != ".pdf" && fileExtension != ".png" && fileExtension != ".tif")
fileExtension = ".jpg";
string projectPath = Enviroment.WebRootPath;
string imagesFilePath = Path.Combine(projectPath, "Images");
if (!Directory.Exists(imagesFilePath))
Directory.CreateDirectory(imagesFilePath);
string fileName = string.Format("demo{0}", fileExtension);
string fullFilePath = Path.Combine(imagesFilePath, fileName);
// save uploaded image file to the server
System.IO.File.WriteAllBytes(fullFilePath, imageFileBytes);
}
}
catch (Exception e)
{
return BadRequest(e.Message);
}
return Content("");
}
}
}
Pls help...Thank's sir...