【问题标题】:.net core 2.2 webapi and handling multypart request.net core 2.2 web api和处理多部分请求
【发布时间】:2020-10-01 15:54:25
【问题描述】:
我有 .net core 2.2 webapi 作为后端。
我的后端必须将来自移动应用程序的请求 (照片和文本) 处理为 multipart-form 请求。
请提示我如何在后端提供它?
【问题讨论】:
-
那里有很多例子。谷歌搜索“net core 2.2 webapi multipart/form-data”得到了一堆循序渐进的教程(例如dottutorials.net/…),我将从那里开始。
标签:
asp.net-core
asp.net-core-webapi
【解决方案1】:
这个问题已经解决了
public class SwaggerFileOperationFilter : IOperationFilter
{
public void Apply(Operation operation, OperationFilterContext context)
{
if (operation.OperationId == "MailWithPhotos")
{
operation.Parameters = new List<IParameter>
{
new NonBodyParameter
{
Name = "awr_file",
Required = false,
Type = "file",
In = "formData"
},
new NonBodyParameter
{
Name = "awr_message",
Required = true,
Type = "string",
In = "formData"
},
new NonBodyParameter
{
Type = "string",
In = "header",
Name = "Authorization",
Description = "token",
Required = true
}
};
}
}
}
[HttpPost]
[ProducesResponseType(typeof(ResponseMail), 200)]
public async Task<PipeResponse> MailWithPhotos([FromForm] MailwithPhoto fIleUploadAPI)
{
var file = fIleUploadAPI.awr_file; // OK!!!
var message = fIleUploadAPI.awr_message; // OK!!!
var tokenA = Request.Headers["Authorization"]; // OK!!!
var fileContentStream11 = new MemoryStream();
await fIleUploadAPI.awr_file.CopyToAsync(fileContentStream11);
await System.IO.File.WriteAllBytesAsync(Path.Combine(folderPath,
fIleUploadAPI.awr_file.FileName),
fileContentStream11.ToArray());
}
public class MailwithPhoto
{
public string awr_message { get; set; }
public string Authorization { get; set; }
public IFormFile awr_file { get; set; }
}