我正在研究asp.net应用程序,我正在从我的视图调用控制器操作。这个视图需要将mp3返回给被调用的函数。什么是Actionresult。被调用的函数返回mp3响应。如何在内存中的api返回的内存中保存mp3响应并返回视图?
这是我的控制器方法代码:
public ActionResult getrecording()
{
byte[] file = new byte[100]; // Convert.ToByte("https://api.abc.com/api/v2/GetRecording?access_token=dfsdfsdfsdfsdfsdfsdfsdfsfsfdsf");
//return (new FileStreamResult(fileStream, "audio/mpeg"));
////return (new FileContentResult(data, "audio/mpeg"));
}
这是视图代码
$("#jquery_jplayer_1").jPlayer({
ready: function (event) {
$(this).jPlayer("setMedia", {
mp3: "/recording/getrecording"
});
},
swfPath: "../../Content/JPlayer/js",
supplied: "mp3",
wmode: "window",
size: 100,
duration: "#duration"
});
答案 0 :(得分:4)
您可以使用WebClient下载远程文件,然后将其传输到响应:
public ActionResult getrecording()
{
var client = new WebClient();
var stream = client.OpenRead("https://api.abc.com/api/v2/GetRecording?access_token=dfsdfsdfsdfsdfsdfsdfsdfsfsfdsf");
return File(stream, "audio/mpeg");
}
如果您希望提示客户端下载文件,只需添加名称:
public ActionResult getrecording()
{
var client = new WebClient();
var stream = client.OpenRead("https://api.abc.com/api/v2/GetRecording?access_token=dfsdfsdfsdfsdfsdfsdfsdfsfsfdsf");
return File(stream, "audio/mpeg", "foo.mp3");
}