3

我正在开发 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"
            });
4

1 回答 1

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");
}
于 2013-03-14T12:40:05.590 回答