36

Rather than displaying a PNG in the browser window, I'd like the action result to trigger the file download dialogue box (you know the open, save as, etc). I can get this to work with the code below using an unknown content type, but the user then has to type in .png at the end of the file name. How can I accomplish this behavior without forcing the user to type in the file extension?

    public ActionResult DownloadAdTemplate(string pathCode)
    {
        var imgPath = Server.MapPath(service.GetTemplatePath(pathCode));
        return base.File(imgPath, "application/unknown");
    }

Solution....

    public ActionResult DownloadAdTemplate(string pathCode)
    {
        var imgPath = Server.MapPath(service.GetTemplatePath(pathCode));
        Response.AddHeader("Content-Disposition", "attachment;filename=DealerAdTemplate.png");
        Response.WriteFile(imgPath);
        Response.End();
        return null;
    }
RSolberg
  • 26,821
  • 23
  • 116
  • 160

6 Answers6

42

I believe you can control this with the content-disposition header.

Response.AddHeader(
       "Content-Disposition", "attachment; filename=\"filenamehere.png\""); 
The Matt
  • 6,618
  • 7
  • 44
  • 61
womp
  • 115,835
  • 26
  • 236
  • 269
11

You need to set the following headers on the response:

  • Content-Disposition: attachment; filename="myfile.png"
  • Content-Type: application/force-download
Aren
  • 54,668
  • 9
  • 68
  • 101
  • Thanks. The content type was needed to force the download as a file. – Hugo Estrada Nov 02 '10 at 14:29
  • content-type is a good solution, content disposition work too, but you can get some risk, exist another post about it https://stackoverflow.com/a/1012461/2062838 – tierrarara Jan 16 '20 at 15:33
7

I actually came here because I was looking for the opposite effect.

    public ActionResult ViewFile()
    {
        string contentType = "Image/jpeg";



        byte[] data = this.FileServer("FileLocation");

        if (data == null)
        {
            return this.Content("No picture for this program.");
        }

        return File(data, contentType, img + ".jpg");
    }
Paul Totzke
  • 1,470
  • 17
  • 33
  • hey, me too! it turns out, we just needed to remove the filename so that your code will look like: return File(data, contentType) – jokab Jan 18 '21 at 23:56
3

With MVC I use a FileResult and return a FilePathResult

public FileResult ImageDownload(int id)
    {
        var image = context.Images.Find(id);
        var imgPath = Server.MapPath(image.FilePath);
        return File(imgPath, "image/jpeg", image.FileName);
    }
Iain M Norman
  • 2,075
  • 15
  • 30
2

The correct way to download file in your case is to use FileResult class.

 public FileResult DownloadFile(string id)
{
try
{
    byte[] imageBytes =  ANY IMAGE SOURCE (PNG)
    MemoryStream ms = new MemoryStream(imageBytes);
    var image = System.Drawing.Image.FromStream(ms);
    image.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
    var fileName = string.Format("{0}.png", "ANY GENERIC FILE NAME");
    return File(ms.ToArray(), "image/png", fileName);
}
catch (Exception)
{
}
return null;
}
NoWar
  • 36,338
  • 80
  • 323
  • 498
1

This I actually @7072k3

var result = File(path, mimeType, fileName);
Response.ContentType = mimeType;
Response.AddHeader("Content-Disposition", "inline");
return result;

Copied that from my working code. This still uses the standard ActionResult return type.