This is the fourth day of my participation in Gwen Challenge
About me
My blog | welcome the attention
preface
At the beginning of Web back and forth analysis, we often encounter the need for file uploads. Uploading user profile pictures, uploading authentication materials, and auditing materials can all be classified as file uploading functions.
Today, the main experience in their development process for a collation, for everyone to learn.
Provide the files in the Web root
The default Web application template calls the UseStaticFiles method in startup. Configure, which will allow static files to be served:
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapDefaultControllerRoute();
});
}
Copy the code
The no-argument UseStaticFiles method overload marks files in the Web root directory as available. The following tag references below/images/myimage.png JPG:
<img src="~/images/MyImage.jpg" class="img" alt="My image" />
Copy the code
In the above code, the waveform character ~/ points to the Web root directory.
Enable static file middleware
By default, static files (such as HTML, CSS, images, and JavaScript) are assets that ASP.NET Core applications provide directly to clients.
Open a static file access, which can be accessed through browser address http://127.0.0.1:8080/images/1.png pictures or other files. This needs to be configured in middleware.
Add a line to the Configure method in startup. cs. Add the default static file middleware.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseStaticFiles();
}
Copy the code
Please refer to official documentation for details:
Docs.microsoft.com/zh-cn/aspne…
File upload function
Create a WebApi project because the front and back are separate projects. Create a controller named FileController and start a simple file upload encoding.
public class FileController : ControllerBase { private static IWebHostEnvironment _webHostingEnvironment; public FileController(IWebHostEnvironment hostingEnvironment) { _webHostingEnvironment = hostingEnvironment; } public IActionResult UploadFile(IFormFile formFile) { //var formFile = Request.Form.Files[0]; / / get request documents sent by var webRootPath = _webHostingEnvironment. WebRootPath; // Application root directory string dirPath = webRootPath + "/UploadFile/" + datetime.now.toString ("yyyyMMdd"); // Application root directory string dirPath = webRootPath + "/UploadFile/" + dateTime.now. // Check whether the saved folder exists. Directory.Exists(dirPath)) { Directory.CreateDirectory(dirPath); } if (formFile == null) {string fileExtesion = path.getextension (formfile.filename); string fileName = Path.GetFileName(formFile.FileName) + "_" + Guid.NewGuid().ToString() + "." + fileExtesion; Using (var fs = system.io.file. Create(webRootPath + "/" + fileName)) {formfile.copyto (fs); } return new JsonResult(new {success = true, errMsg = "file upload successful"}); } else {return new JsonResult(new {success = false, errMsg = "upload file not detected"}); }}}Copy the code