Skip to content Skip to sidebar Skip to footer

How To Display Open/save Dialog Asp Net Mvc 4

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/sav

Solution 1:

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 browserreturn File(filePath, contentType, "Report.pdf");
}

Solution 2:

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 browserreturn File(filePath, contentType, "Report.pdf");
}

Post a Comment for "How To Display Open/save Dialog Asp Net Mvc 4"