6

I am able to request a file and also have it returned. I don´t know how to display a open/save dialog box.

View:

function saveDocument() {
    $.ajax({
        url: '/Operacao/saveDocument',
        type: 'POST',
        DataType: "html",
        success: function (data) {
            //I get the file content here
        }
    });
}

Controller:

public void saveDocument() {
    Response.ContentType = "image/jpeg";
    Response.AppendHeader("Content-Disposition", "attachment; filename=SailBig.jpg");
    Response.TransmitFile(Server.MapPath("~/MyPDFs/Pdf1.pdf"));    
    Response.End();
}
Cerbrus
  • 70,800
  • 18
  • 132
  • 147
Guilherme Longo
  • 2,278
  • 7
  • 44
  • 64

2 Answers2

8

I think you cannot download a file in a browser async, just redirect the user to the action and the browser will open a save dialog window. In asp.net mvc you could have an action method to download a file resulting in a FileResult with the File method of the base controller.

public ActionResult SaveDocument()
{   
    string filePath = Server.MapPath("~/MyPDFs/Pdf1.pdf");
    string contentType = "application/pdf";

    //Parameters to file are
    //1. The File Path on the File Server
    //2. The content type MIME type
    //3. The parameter for the file save by the browser

    return File(filePath, contentType, "Report.pdf");
}
Felipe Oriani
  • 37,948
  • 19
  • 131
  • 194
  • 2
    It is downloading automatically without asking. The dialog does not show! – Guilherme Longo Jan 03 '13 at 13:07
  • 3
    That is browser dependent. If you set to download automatically to a given folder, the browser will download automatically. Firefox and Chrome are some browsers with this behavior. – João Simões Jan 03 '13 at 13:09
  • @JoãoSimões Is there any way to `get Save As dialogue box` to appear in Firefox and Chrome? I am yet searching for solution! – Shubh Oct 21 '13 at 10:31
  • 1
    For Chrome: In Settings, Click Show advanced settings and scroll down to the "Downloads" section. To change the default download location, click Change and select where you'd like your files to be saved. If you'd rather choose a specific location for each download, select the "Ask where to save each file before downloading" checkbox. – jaybro Dec 07 '15 at 16:51
  • For Firefox: https://support.mozilla.org/en-US/kb/change-firefox-behavior-when-open-file – jaybro Dec 07 '15 at 16:53
1

One way to force firefox (doen't work for chrome) to open the save dialogue is to set the contenttype to "application/octet-stream" and give it a filename with the correct extension.

public ActionResult SaveDocument()
{   
    string filePath = Server.MapPath("~/MyPDFs/Pdf1.pdf");
    string contentType = "application/octet-stream";  //<---- This is the magic

    //Parameters to file are
    //1. The File Path on the File Server
    //2. The content type MIME type
    //3. The parameter for the file save by the browser

    return File(filePath, contentType, "Report.pdf");
}
Zaphod
  • 1,412
  • 2
  • 13
  • 27