【发布时间】:2021-02-26 05:58:08
【问题描述】:
试图通过我创建的 API 在浏览器中下载 mp3 文件。但不是接收 mp3 文件。我不断收到 JSON 格式的响应。我已经从return-file-in-asp-net-core-web-api 的答案中提到过,但我仍然无法下载 mp3 文件。 有没有我忽略的错误,请帮忙?
这是我从 UI 下载的方法
void DownloadRecording(RecordingHistory voicehistory)
{
try
{
using (var client = new WebClient())
{
client.DownloadFile("https://2d489fd863a2.ngrok.io/api/download/" + voicehistory.RecordingId + ".mp3", voicehistory.RecordingId + ".mp3");
}
}
catch { }
}
这是我从服务器下载mp3的api函数
[HttpGet("download/{recordingFile}")]
public async Task<IActionResult> DownloadVoiceRecording(string recordingFile)
{
string filePath = Directory.GetCurrentDirectory() + @"\audio\Processed\" + recordingFile;
var memory = new MemoryStream();
using (var stream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read))
{
await stream.CopyToAsync(memory);
}
memory.Position = 0;
var types = GetMimeTypes();
var ext = Path.GetExtension(filePath).ToLowerInvariant();
return File(filePath, types[ext], recordingFile);
}
private Dictionary<string, string> GetMimeTypes()
{
return new Dictionary<string, string>
{
{".mp3", "audio/mpeg"},
{".wav","audio/wav" }
};
}
这是我从浏览器和 Postman 得到的响应
{
"Version": "2.0.0.0",
"StatusCode": 200,
"Message": "Status 200 OK",
"Result":"��@� ... ... /// A lot of random symbol here
}
【问题讨论】:
标签: rest .net-core asp.net-core-webapi mp3 webclient