2013-01-03 17 views
5

कैसे प्रदर्शित करें मैं एक फाइल का अनुरोध करने में सक्षम हूं और इसे वापस कर चुका हूं। मुझे नहीं पता कि एक खुला/सहेजें संवाद बॉक्स कैसे प्रदर्शित करें।ओपन/सेव डायलॉग एएसपी नेट एमवीसी 4

दृश्य:

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

नियंत्रक:

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

उत्तर

7

मुझे लगता है कि आप एक ब्राउज़र async में एक फ़ाइल डाउनलोड नहीं कर सकते, सिर्फ कार्रवाई करने के लिए उपयोगकर्ता अनुप्रेषित और ब्राउज़र खुल जाएगा एक सहेजें संवाद विंडो। एएसपीएनटी एमवीसी में आप फ़ाइल नियंत्रक के File विधि के साथ FileResult में फ़ाइल डाउनलोड करने के लिए एक क्रिया विधि हो सकती है।

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

बहुत बहुत धन्यवाद !!! –

+1

यह पूछे बिना स्वचालित रूप से डाउनलोड हो रहा है। संवाद नहीं दिखाता है! –

+3

वह ब्राउज़र निर्भर है। यदि आप किसी दिए गए फ़ोल्डर में स्वचालित रूप से डाउनलोड करने के लिए सेट करते हैं, तो ब्राउज़र स्वचालित रूप से डाउनलोड हो जाएगा। फ़ायरफ़ॉक्स और क्रोम इस व्यवहार के साथ कुछ ब्राउज़र हैं। –

1

एक ही रास्ता (क्रोम के लिए काम करते doen't) संवाद बचाने को खोलने के लिए मजबूर करने के लिए फ़ायरफ़ॉक्स contenttype को "application/ओकटेट धारा" सेट और यह सही विस्तार के साथ एक फ़ाइल नाम दे रहा है।

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"); 
} 
संबंधित मुद्दे