RestClient发送IFormFile

时间:2018-03-08 14:09:10

标签: c# asp.net-core multipartform-data restsharp

我正在接收来自我的控制器的文件

 public async Task<IActionResult> ScanFile(IFormFile request)

如何在RestSharp中传递它,如下面 AddParameter 所示。 我尝试设置自己的边界并序列化IFormFile没有解决。

var client = new RestClient("https://api_url/requests");
var request = new RestRequest(Method.POST);
request.AddHeader("authorization", "bearer API_TOKEN");
request.AddHeader("accept", "application/json");
request.AddHeader("content-type", "multipart/form-data; boundary=---011000010111000001101001");
request.AddParameter("multipart/form-data; boundary=---011000010111000001101001", "-----011000010111000001101001\r\nContent-Disposition: form-data; name=\"file\"; filename=\"eicar.com\"\r\nContent-Type: text/plain\r\n\r\nX5O!P%@AP[4\\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*\r\n-----011000010111000001101001--", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);

1 个答案:

答案 0 :(得分:0)

这对我有用。

在我的控制台应用中,

var client = new RestClient("http://localhost:30000/api/Factory");
var request = new RestRequest("UploadDocumentBlob", Method.POST) { RequestFormat = DataFormat.Json, AlwaysMultipartFormData = true };
request.AddFile("file", Path.Combine(settings.PdfStorePath, "11rwai4q.pdf"));
var uploadDocumentBlobResponse = client.Post<Response>(request).Data;

访问我的api

[HttpPost("api/[controller]/[action]")]
public async Task<Response> UploadDocumentBlob(IFormFile file)

关键是在request.AddFile中,第一个参数必须为"file",而不是其他任何名称。

希望这对某人有帮助。