Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASPNetCore - Uploading a file through REST

Tags:

I am using Insomnia for testing an API, but the same happens with Postman.

I want to test a file upload, with the following controller:

public async Task<IActionResult> Post([FromForm]IFormFile File)

If I set the request as a multipart request:

enter image description here

it works.

However, if I set it as a binary file:

enter image description here

I don't know how to get the data. How can it be done?

Also, in the controller method's signature, if I change [FromForm] to [FromBody], I'm not getting data.

Can someone clarify this for me?

like image 409
Thomas Avatar asked May 07 '18 23:05

Thomas


People also ask

How do I upload files to IFormFile?

Upload Single FileTo add view, right click on action method and click on add view. Then select View from left side filter and select Razor View – Empty. Then click on Add button. Create design for your view as per your requirements.


2 Answers

As you've noticed already, using binary file option in Postman/Insomnia doesn't work the standard way. There are three different ways to upload file via RESTful API, and you have to choose one.

I've included code snippets that read the uploaded file contents to a string and output it -- try sending a text file, and you should get the contents of the file in the 200 response.

Form-data upload

This is the most popular/well-known upload method formatting the data you send as a set of key/value pairs. You normally need to specify Content-Type to multipart/form-data in the request, and then use [FromForm] attribute in MVC to bind values to variables. Also, you can use the built-in IFormFile class to access the file uploaded.

[HttpPost]
public async Task<IActionResult> PostFormData([FromForm] IFormFile file)
{
    using (var sr = new StreamReader(file.OpenReadStream()))
    {
        var content = await sr.ReadToEndAsync();
        return Ok(content);
    }
}

Body upload

You can send body in the format that MVC understands, e.g. JSON, and embed the file inside it. Normally, the file contents would be encoded using Base64 or other encoding to prevent character encoding/decoding issues, especially if you are sending images or binary data. E.g.

{
    "file": "MTIz"
}

And then specify [FromBody] inside your controller, and use class for model deserialization.

[HttpPost]
public IActionResult PostBody([FromBody] UploadModel uploadModel)
{
    var bytes = Convert.FromBase64String(uploadModel.File);
    var decodedString = Encoding.UTF8.GetString(bytes);
    return Ok(decodedString);
}
// ...
public class UploadModel
{
    public string File { get; set; }
}

When using large and non-text files, the JSON request becomes clunky and hard to read though.

Binary file

The key point here is that your file is the whole request. The request doesn't contain any additional info to help MVC to bind values to variables in your code. Therefore, to access the file, you need to read Body in the Request.

[HttpPost]
public async Task<IActionResult> PostBinary()
{
    using (var sr = new StreamReader(Request.Body))
    {
        var body = await sr.ReadToEndAsync();
        return Ok(body);
    }
}

Note: the example reads Body as string. You may want to use Stream or byte[] in your application to avoid file data encoding issues.

like image 135
Ignas Avatar answered Sep 28 '22 21:09

Ignas


In addition of the above, in case of multipart file conversion to base64String you can refer to the below:

if (File.Length> 0)
   {
       using (var ms = new MemoryStream())
       {
           File.CopyTo(ms);
           var fileBytes = ms.ToArray();
           string s = Convert.ToBase64String(fileBytes);                                    
       }
    }

Note: I am using this code for .NET CORE 2.1

like image 20
AlessHo Avatar answered Sep 28 '22 21:09

AlessHo