如何使用HttpClient()将音频文件上传到服务器。 我的音频文件格式是“testaudio.3gpp” 我想用
来做await httpClient.PostAsync(uploadServiceBaseAddress, content);
请建议我的解决方案。 完整代码
string path = "sdcard/testaudio.3gpp";
var content = new MultipartFormDataContent();
var httpClient = new HttpClient();
var uploadServiceBaseAddress = "https://example.com/UploadaudioFile.aspx";
var httpResponseMessage = await httpClient.PostAsync(uploadServiceBaseAddress, content);
string resp = await httpResponseMessage.Content.ReadAsStringAsync();
我认为流内容或其他内容必须添加到内容变量中。
答案 0 :(得分:0)
如下:
string path = "sdcard/testaudio.3gpp";
var content = new MultipartFormDataContent();
var audioContent = new ByteArraycontent(System.IO.File.ReadAllBytes(path));
audioContent.Headers.ContentType = MediaTypeHeaderValue.Parse("audio/3gpp");
content.Add(audioContent, "audio", "testaudio.3gpp");
var httpClient = new HttpClient();
var uploadServiceBaseAddress = "https://example.com/UploadaudioFile.aspx";
var httpResponseMessage = await httpClient.PostAsync(uploadServiceBaseAddress, content);
string resp = await httpResponseMessage.Content.ReadAsStringAsync();