Implementation of asp. net core webapi File Upload Function

  • 2021-11-29 06:37:57
  • OfStack

Recently, a new project was developed, which used asp. net core 2.0, webapi as the background and postgresql as the database. The first problem is the problem of uploading files.

1 Pits in POST File

After the controller of webapi is created using the default template, the post request has the default


 // POST api/values
  [HttpPost]
  public void Post([FromBody]string value)
  {
  }

The request uses the [FromBody] Tag used to indicate that data is obtained from the request body.

For file upload requests, use it directly in this Post function Request.Form.Files It can't be done, and it can't be routed successfully.
Typical upload, you need to set the request sent by the front end Content-Type For multipart/form-data And then add attribute decorations to the controller class:


 [Produces("application/json")]
 [Consumes("application/json", "multipart/form-data")]// Here is the new 
 [Route("api/[controller]")]
 public class FileController : Controller

Indicates that the ES 28EN can accept multipart/form-data Data in the form. Correspondingly, the code for modifying post is as follows:


 // POST: api/File
  [HttpPost]
  public Task<ActionResult> Post(IFormCollection files)

Note here that IFormCollection is used. This is the type collection of IForm, which is actually Request.Form .

Note that many places write that you can use IFormFile and write it directly as


// POST: api/File
  [HttpPost]
  public Task<ActionResult> Post(IFormFile file)

There is no way to get the object in the actual test, file is normally null, or my method is wrong.

Then you can use files. Files to enumerate files in post method. Each file is an IFormFile object, and you can flexibly use common attributes such as FileName, Name and Length. Of course, we can also take no parameters:


// POST: api/File
  [HttpPost]
  public Task<ActionResult> Post()

Use Request. Form. Files directly to obtain file data.

P. S. For IFormFile, the same as System.IO.File Object, IFormFile lacks many methods and only provides OpenReadStream() Method, which returns 1 stream object. Many API reading files can accept stream as an alternative to FilePath.

Upload other data at the same time

1-like file upload request, not only upload file data, but also usually need to upload other file information data (such as file type, uploader, etc.). Modify the post method under 1 to read as follows:


 [HttpPost]
  public Task<ActionResult> Post([FromBody]string type,IFormCollection files)

Package all type into the request and send it again. Found that...... type is null.

MSDN said, The reason for this rule is that in in in in in in in stored in a non-buffered stream that can only be read once once once once once once once

So, [FromBody] can only add one, but I did only add one here. Is there a problem? Obviously, that IFormCollection is also parsed by default through [FromBody], so the correct method is not to add [FromBody].


 [HttpPost]
  public Task<ActionResult> Post(string type,IFormCollection files)

Postscript

Recall that when I was doing WebService, I uploaded the file and wrote it
Multi-platform upload
I feel a lot. I used to transcode it to base64 and send it through string. Now it is direct type identification...


Related articles: