Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC: Retrieving an .mp3 file and returning it to the user

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;
    }

I have emptied out the function and method because I have tried several different things to accomplish this: I need to access an audio file from a local location and allow the user to listen to it/download it upon clicking the Listen link.

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

like image 834
Ron Saylor Avatar asked Jan 24 '26 05:01

Ron Saylor


1 Answers

Here is the final result:

    [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");
    }

On the page, the link is:

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

Kudos to my boss for the help.

like image 150
Ron Saylor Avatar answered Jan 26 '26 18:01

Ron Saylor



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!