ASPNetCore-通过REST上传文件 [英] ASPNetCore - Uploading a file through REST
问题描述
我正在使用Insomnia来测试API,但是Postman也是如此。
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:
它有效。
但是,如果我将其设置为二进制文件:
However, if I set it as a binary file:
我不知道如何获取数据。
I don't know how to get the data. How can it be done?
此外,在控制器方法的签名中,如果我将[FromForm]更改为[FromBody],则不会获取数据。
Also, in the controller method's signature, if I change [FromForm] to [FromBody], I'm not getting data.
有人可以帮我澄清一下吗?
Can someone clarify this for me?
推荐答案
您已经注意到了,在Postman / Insomnia中使用二进制文件
选项无法正常工作。通过RESTful API上传文件的方式有三种,您必须选择一种。
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.
我提供了一些代码片段,这些片段将上传的文件内容读取为一个字符串,并且输出它-尝试发送文本文件,您应该在200响应中获取文件的内容。
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.
表格数据上传
这是最流行/最著名的上载方法,用于将您发送的数据格式化为一组键/值对。通常,您需要在请求中指定 Content-Type
到 multipart / form-data
,然后使用<$ c MVC中的$ c> [FromForm] 属性可将值绑定到变量。另外,您可以使用内置的 IFormFile
类访问上载的文件。
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);
}
}
正文上传
您可以使用MVC可以理解的格式发送正文,例如JSON,并将文件嵌入其中。通常,文件内容将使用Base64或其他编码进行编码,以防止出现字符编码/解码问题,尤其是在发送图像或二进制数据时。例如,
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"
}
然后在控制器内指定 [FromBody]
,并使用类进行模型反序列化。
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; }
}
使用大型非文本文件时,JSON请求变得笨拙,
When using large and non-text files, the JSON request becomes clunky and hard to read though.
二进制文件
这里的关键是您的文件是整个请求。该请求不包含任何其他信息,以帮助MVC将值绑定到代码中的变量。因此,要访问该文件,您需要在 Request
中读取 Body
。
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);
}
}
这篇关于ASPNetCore-通过REST上传文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!