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.
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
reference
ASP.NET Core uploads and saves single and multiple files to the server
The END
The end of this article, I hope to help you 😃
More exciting technical articles summary in my public number programmer tool set, continue to update, welcome to pay attention to the subscription collection.
If there are any questions or suggestions, you can exchange more original articles, writing is limited, talent and learning is shallow, if there is something wrong in the article, hope to inform.
Welfare kyi
Public account background reply: “pay attention to gift package”, get the value of 5K video learning resources
Public number background reply: “skill map”, send you a most complete developer skill map