Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8022325
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T22:14:05+00:00 2026-06-04T22:14:05+00:00

I am trying to create download functionality in an MVC action, and the actual

  • 0

I am trying to create download functionality in an MVC action, and the actual download works, but the file is saved with the actionname as the filename e.g. from the code below I get a filename of ‘DownloadMP3’. Does anybody know how to keep the original filename when downloading?

[Authorize]
    public virtual FileResult DownloadMP3(string fileName)
    {
        //Actions/Download?filename=test

        //test 
        string filePath = @"~/Content/xxxxx/" + fileName + ".mp3";

        Response.AddHeader("content-disposition", "attachment;" + filePath + ";");

        return File(filePath, "audio/mpeg3");

        //for wav use audio/wav

    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T22:14:08+00:00Added an answer on June 4, 2026 at 10:14 pm

    The correct syntax of the Content-Disposition header is like this:

    Content-Disposition: attachment; filename=foo.mp3
    

    But in this case you could simply use the overload of the File method which takes 3 arguments, the third being the filename. It will automatically emit the Content-Disposition header with attachment so that you don’t need to manually add it (and make mistakes in its syntax as you did).

    Also your filePath variable must point to the physical file on the server and not a relative url. Use Server.MapPath for this:

    public virtual ActionResult DownloadMP3(string fileName)
    {
        var downloadPath = Server.MapPath("~/Content/xxxxx/");
        fileName = Path.ChangeExtension(Path.GetFileName(fileName), "mp3");
        var file = Path.Combine(downloadPath, fileName);
        return File(file, "audio/mpeg3", fileName);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to create a force-download page to prevent browsers from opening files
I'm trying to create a Makefile that will download and process file a file
I'm trying to create a custom DataGridViewButtonCell to download a file once clicked. That
I am trying to create a PDF file using struts 2.Action class location is
I`m trying to create a file downloader as a background service but when a
I'm trying to create a file download page. This page when requested should prompt
I am trying to create a google-chrome-extension that will download an mp3 file. I
I'm trying to create a code to download mp3 files embedded in a page.
Trying to create a black line in my view to separate text blocks but
Trying to create a new Dedicated Cache Role in Windows Azure but get the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.