[英]C# Core 1.1 Web API Receive zip file
I created Web API endpoint to receive a zip file which looks like this 我创建了Web API端点来接收看起来像这样的zip文件
[HttpPut()]
[Consumes("application/zip")]
public async Task<IActionResult> ImportZip()
{
var zipFile = HttpContext.Request.Form.Files.FirstOrDefault();
....
And I'm trying to test it via Postman with such request: 我正在尝试通过邮递员对此请求进行测试:
But I'm getting exception "System.InvalidOperationException: Incorrect Content-Type: application/zip" 但我收到异常“ System.InvalidOperationException:错误的内容类型:应用程序/邮政编码”
What I'm doing wrong? 我做错了什么? Thanks!
谢谢!
EDIT Request actually passes through [Consumes("application/zip")] attribute, but crashing on "HttpContext.Request.Form.Files.FirstOrDefault();" 编辑请求实际上通过[Consumes(“ application / zip”)]属性传递,但在“ HttpContext.Request.Form.Files.FirstOrDefault();”上崩溃
EDIT2 Ok, so finally I successfully received a file when I didn't put any header in the request and removed [Consumes("application/zip")] attribute. EDIT2好,所以当我在请求中未放置任何标头并删除了[Consumes(“ application / zip”)]属性时,终于可以成功接收文件。 In
Request.Form.Files
my file have a type "application/x-zip-compressed" but when I'm trying to use it in headers content-type and in Consumes attribute I get same crash System.InvalidOperationException: Incorrect Content-Type: application/x-zip-compressed
在
Request.Form.Files
我的文件的类型为“ application / x-zip-compressed”,但是当我尝试在标头content-type和Consumes属性中使用它时,出现相同的崩溃System.InvalidOperationException: Incorrect Content-Type: application/x-zip-compressed
You can use IFormFile
to transfer your zip. 您可以使用
IFormFile
传输您的zip。 Your WebApi endpoint must be updated to this: 您的WebApi端点必须更新为此:
[HttpPut]
[Consumes("multipart/form-data")]
public void Put(IFormFile file)
{
var stream = file.OpenReadStream();
}
Postman request should look something like this (You don't need Content-Type
header as it gets resolved on request): 邮递员的请求应如下所示(您不需要
Content-Type
标头,因为它可以根据请求进行解析):
Postman will send multipart/form-data
as Content-Type
. 邮递员将
multipart/form-data
作为Content-Type
。 Normaly you only specify the Consumes
attribute when the web api action has to support multiple content types or when you wan't to support a specific content type. 通常,只有在Web api操作必须支持多种内容类型时或者在不支持特定内容类型时才指定
Consumes
属性。
As your web api action depends on HttpContext.Request.Form.Files
the Content-Type
has to be multipart/form-data
anyway. 由于您的Web api操作取决于
HttpContext.Request.Form.Files
因此Content-Type
无论如何都必须是multipart/form-data
。
The best option to only allow zip files is to try parsing it. 仅允许zip文件的最佳选择是尝试对其进行解析。 If it fails you know a wrong file has been uploaded.
如果失败,则说明上传了错误的文件。
[HttpPut("api/import")]
public IActionResult ImportZip()
{
var file = Request.Form.Files.FirstOrDefault();
if (file == null)
return BadRequest();
try
{
using (var zip = new ZipArchive(file.OpenReadStream()))
{
// do stuff with the zip file
}
}
catch
{
return BadRequest();
}
return Ok();
}
Another option would be to check the BOM (Byte order mark) of the file. 另一个选择是检查文件的BOM(字节顺序标记) 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.