2013-01-03 18 views
5

Bir dosya isteğinde bulunabilir ve ayrıca döndürebilir. Açık/kaydet iletişim kutusunu nasıl görüntüleyeceğimi bilmiyorum.Açma/kaydetme iletişim kutusu nasıl görüntülenir asp net mvc 4

Görünüm:

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

Denetleyici:

public void saveDocument() { 
    Response.ContentType = "image/jpeg"; 
    Response.AppendHeader("Content-Disposition", "attachment; filename=SailBig.jpg"); 
    Response.TransmitFile(Server.MapPath("~/MyPDFs/Pdf1.pdf"));  
    Response.End(); 
} 

cevap

7

Ben sadece eyleme kullanıcıyı yönlendirmek ve tarayıcı açılacak bir tarayıcı zaman uyumsuz bir dosya indiremezsiniz düşünüyorum bir kaydetme penceresi. Asp.net mvc'de, temel denetleyicinin File yöntemiyle FileResult sonuçlanan bir dosyayı indirmek için bir işlem yönteminiz olabilir. diyalog kaydetmek açmak için (krom için çalışma doent) firefox zorlamak için

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

Çok teşekkürler! –

+1

Sormadan otomatik olarak indiriliyor. Diyalog göstermiyor! –

+3

Bu, tarayıcıya bağımlı. Belirli bir klasöre otomatik olarak indirmeyi ayarlarsanız, tarayıcı otomatik olarak indirilecektir. Firefox ve Chrome, bu davranışa sahip bazı tarayıcılardır. –

1

bir yolu ContentType'ı için "application/octet-stream" set ve ona doğru uzantılı bir dosya adı vermektir.

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"); 
} 
İlgili konular