ASP.NET MVC:检索.mp3文件并返回给用户 [英] ASP.NET MVC: Retrieving an .mp3 file and returning it to the user

查看:213
本文介绍了ASP.NET MVC:检索.mp3文件并返回给用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在有一个包含链接的HTML页面:

I currently have an HTML page that contains a link:

<a href="javascript:void()" onclick="return getAudioFile('fileLocation');">Listen</a>

点击试听链接调用这个函数:

Clicking the "Listen" link calls this function:

    function getRecordingFile(fileLocation) {

    };

最终应调用该控制器的方法:

Which should ultimately call this controller method:

    [HttpPost]
    public ActionResult GetAudioFile(string fileLocation)
    {
        return null;
    }

我已经掏空了的功能和方法,因为我已经尝试了几种不同的事情来实现:我需要从本地位置访问的音频文件,并允许用户听吧/下载后点击收听链接。

什么似乎是去了解这一点的最好方法是什么?

What seems to be the best way to go about this?

推荐答案

下面是最终的结果是:

    [HttpGet]
    public ActionResult GetAudioFile(string fileLocation)
    {
        var bytes = new byte[0];


        using (var fs = new FileStream(fileLocation, FileMode.Open, FileAccess.Read)
        {
            var br = new BinaryReader(fs);
            long numBytes = new FileInfo(fileLocation).Length;
            buff = br.ReadBytes((int)numBytes);
        }

        return File(buff, "audio/mpeg", "callrecording.mp3");
    }

在该页面,链接是:

<a href="/Controller/GetAudioFile?fileName=@fileLocation">Listen</a>

荣誉给我的老板的帮助。

Kudos to my boss for the help.

这篇关于ASP.NET MVC:检索.mp3文件并返回给用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆