【发布时间】:2019-07-02 07:16:05
【问题描述】:
正如标题所说,我正在尝试将 .zip 从我的 WPF 应用程序上传到我的 .NET Core Web API。
经过一些研究,我发现可以使用 MultiPartDataContent 并以这种方式发送。
向服务器发送数据的代码如下所示:
{
client.BaseAddress = new Uri("http://localhost:62337/");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
string filepath = @"C:\Users\uic10950\Desktop\Meeting2Text\RecordingApp\bin\mixedaudio.zip";
string filename = "mixedaudio.zip";
MultipartFormDataContent content = new MultipartFormDataContent();
ByteArrayContent fileContent = new ByteArrayContent(System.IO.File.ReadAllBytes(filepath));
fileContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment") { FileName = filename };
content.Add(fileContent);
HttpResponseMessage response = await client.PostAsync("api/transcriptions/recording", content);
string returnString = await response.Content.ReadAsAsync<string>();
}
在服务器端,我的控制器操作如下所示:
public async Task<IActionResult> AddFileToTranscription([FromForm] IFormFile file)
{
using (var sr = new StreamReader(file.OpenReadStream()))
{
var content = await sr.ReadToEndAsync();
return Ok(content);
}
}
文件上传工作后,此操作代码将立即更改
根据我在研究时的理解,我的文件应该在以下位置找到:
[FromForm] IFormFile
action的file属性,可惜是null。
【问题讨论】:
-
你试过没有
[FromForm]吗? -
@Ali Bahrami 还是同样的问题,它保持为空
标签: c# wpf asp.net-core-webapi dotnet-httpclient