![](/img/trans.png)
[英]How to upload files using IFormFile in C# Core with input type as file?
[英]How to upload file outside or controller and not in IFormFile type
我使用 Mediatr 處理從 WebApi 項目發送的應用程序項目中的命令和查詢。 在 WebApi 項目中用戶發送到 controller IFormFile。 要在應用程序項目中處理 IFormFile,我必須安裝 as.netcore.http.features。 但我不希望在應用程序項目中存在這種依賴性。 如何將不在 IFormFile 中的用戶文件從 controller 發送到應用程序項目的處理程序?
您可以將文件的內容讀入字節數組並將字節數組作為參數傳遞給您的 Mediatr 請求。
[HttpPost]
public async Task<IActionResult> Post([FromForm]IFormFile file)
{
using (var stream = new MemoryStream())
{
await file.CopyToAsync(stream);
var fileContents = stream.ToArray();
var result = await _mediator.Send(new YourMediatrRequest(fileContents));
return Ok(result);
}
}
public class YourMediatrRequestHandler : IRequestHandler<YourMediatrRequest, YourMediatrResponse>
{
public Task<YourMediatrResponse> Handle(YourMediatrRequest request, CancellationToken cancellationToken)
{
var fileContents = request.FileContents;
...more code
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.