6

मैं वर्तमान में अपने सर्वर से फ़ाइलों को डाउनलोड करने के लिए एक HttpResponse का उपयोग कर रहा हूं। मेरे पास एक्सेल/वर्ड फ़ाइलों को डाउनलोड करने के लिए पहले से ही कुछ फ़ंक्शन का उपयोग किया जा रहा है, लेकिन मुझे डाउनलोड करने के लिए मेरी सरल टेक्स्ट फ़ाइल (.txt) प्राप्त करने में परेशानी हो रही है।Response.TransmitFile डाउनलोड नहीं कर रहा है, और कोई त्रुटि नहीं फेंक रहा है

टेक्स्ट फ़ाइल के साथ मैं मूल रूप से एक टेक्स्टबॉक्स में सामग्री को डंप कर रहा हूं, फ़ाइल को HttpResponse के साथ फ़ाइल डाउनलोड करने का प्रयास कर रहा हूं और फिर अस्थायी टेक्स्ट फ़ाइल को हटा सकता हूं। यहाँ

protected void linkInstructions_Click(object sender, EventArgs e) 
{ 
    String FileName = "BulkAdd_Instructions.doc"; 
    String FilePath = Server.MapPath("~/TempFiles/BulkAdd_Instructions.doc"); 
    System.Web.HttpResponse response = System.Web.HttpContext.Current.Response; 
    response.ClearContent(); 
    response.Clear(); 
    response.ContentType = "application/x-unknown"; 
    response.AddHeader("Content-Disposition", "attachment; filename=" + FileName + ";"); 
    response.TransmitFile(FilePath); 
    response.Flush(); 
    response.End(); 
} 

और कोड वह काम नहीं करता का हिस्सा है:

यहाँ मेरी कोड का एक उदाहरण है कि एक्सेल/Word दस्तावेज़ों के लिए काम करता है।
ध्यान दें कि कोड किसी भी त्रुटि को फेंकने के बिना चलता है। फ़ाइल बनाई गई है, और हटा दी गई है, लेकिन उपयोगकर्ता को कभी नहीं छोड़ा गया।

protected void saveLog(object sender, EventArgs e) 
{ 
    string date = DateTime.Now.ToString("MM_dd_yyyy_hhmm");  // Get Date/Time 
    string fileName = "BulkLog_"+ date + ".txt";    // Stitch File Name + Date/Time 
    string logText = errorLog.Text;        // Get Text from TextBox 
    string halfPath = "~/TempFiles/" + fileName;    // Add File Name to Path 
    string mappedPath = Server.MapPath(halfPath);    // Create Full Path 

    File.WriteAllText(mappedPath, logText);      // Write All Text to File 

    System.Web.HttpResponse response = System.Web.HttpContext.Current.Response; 
    response.ClearContent(); 
    response.Clear(); 
    response.ContentType = "text/plain"; 
    response.AddHeader("Content-Disposition", "attachment; filename=" + fileName); 
    response.TransmitFile(mappedPath);    // Transmit File 
    response.Flush(); 

    System.IO.File.Delete(mappedPath);    // Delete Temporary Log 
    response.End(); 
} 

उत्तर

-5

इस्तेमाल किया मैं अपने दम पर समस्या का समाधान समाप्त हो गया है। यह पता चला कि यह एक अजाक्स समस्या थी जो मेरे बटन को ठीक से पोस्टबैक करने की इजाजत नहीं दे रही थी। इसने ट्रांसमिटफाइल को निकाल दिया जाने से रोक दिया।

सहायता के लिए धन्यवाद!

+0

आपने इसे कैसे हल किया? मेरे पास भी बिलकुल ऐसा ही मुद्दा है। मेरा ModalpopupExtender/UpdatePanel मेरे बटन को रोकता है जो मेरे दायर डाउनलोड को काम करने से रोकता है। जब मैं अपने मोडलपॉपअपेंडर/अपडेट पैनेल के बाहर उस बटन को ले जाता हूं, तो यह बेकार ढंग से काम करता है। – JoeManiaci

+3

हमें यह बताने के लिए धन्यवाद कि आपने इसे ठीक किया है और कैसे मदद नहीं कर रहा है। – Danrex

+0

हमें बता रहा है कि आपने कैसे तय किया है यह अच्छा होगा – Nevyn

10

ऐसा इसलिए है क्योंकि आप इसे भेजने से पहले फ़ाइल को हटा रहे हैं।

MSDN से - HttpResponse.End Method

सभी वर्तमान ग्राहक को आउटपुट बफ़र भेजता है, पेज के निष्पादन बंद हो जाता है, और EndRequest घटना उठाती है।

अपना सिस्टम डालने का प्रयास करें .IO.File.Delete (मैप्डपाथ); प्रतिक्रिया के बाद लाइन। अंत(); मेरे परीक्षण में बस यह काम करने लग रहा था।

साथ ही, यह जांचने का एक अच्छा विचार हो सकता है कि फ़ाइल पहले मौजूद है या नहीं, कोई फ़ाइल नहीं देख सकता। वहां मौजूद है, कोई शून्य संदर्भ अपवाद नहीं चाहते हैं, और सामग्री-लंबाई सेट करने के लिए।

संपादित करें: यहां एक कोड है जिसे मैंने कुछ समय पहले एक परियोजना में इस्तेमाल किया था, थोड़ा सा आपकी मदद कर सकता है।

// Get the physical Path of the file 
string filepath = System.Web.Hosting.HostingEnvironment.ApplicationPhysicalPath + folder + filename; 

// Create New instance of FileInfo class to get the properties of the file being downloaded 
FileInfo file = new FileInfo(filepath); 

// Checking if file exists 
if (file.Exists) 
{        
    // Clear the content of the response 
    Response.ClearContent(); 

    // LINE1: Add the file name and attachment, which will force the open/cance/save dialog to show, to the header 
    Response.AddHeader("Content-Disposition", String.Format("attachment; filename={0}", file.Name)); 

    // Add the file size into the response header 
    Response.AddHeader("Content-Length", file.Length.ToString()); 

    // Set the ContentType 
    Response.ContentType = ReturnFiletype(file.Extension.ToLower()); 

    // Write the file into the response (TransmitFile is for ASP.NET 2.0. In ASP.NET 1.1 you have to use WriteFile instead) 
    Response.TransmitFile(file.FullName); 

    // End the response 
    Response.End(); 

    //send statistics to the class 
} 

और यहाँ Filetype विधि मैं

//return the filetype to tell the browser. 
//defaults to "application/octet-stream" if it cant find a match, as this works for all file types. 
public static string ReturnFiletype(string fileExtension) 
{ 
    switch (fileExtension) 
    { 
     case ".htm": 
     case ".html": 
     case ".log": 
      return "text/HTML"; 
     case ".txt": 
      return "text/plain"; 
     case ".doc": 
      return "application/ms-word"; 
     case ".tiff": 
     case ".tif": 
      return "image/tiff"; 
     case ".asf": 
      return "video/x-ms-asf"; 
     case ".avi": 
      return "video/avi"; 
     case ".zip": 
      return "application/zip"; 
     case ".xls": 
     case ".csv": 
      return "application/vnd.ms-excel"; 
     case ".gif": 
      return "image/gif"; 
     case ".jpg": 
     case "jpeg": 
      return "image/jpeg"; 
     case ".bmp": 
      return "image/bmp"; 
     case ".wav": 
      return "audio/wav"; 
     case ".mp3": 
      return "audio/mpeg3"; 
     case ".mpg": 
     case "mpeg": 
      return "video/mpeg"; 
     case ".rtf": 
      return "application/rtf"; 
     case ".asp": 
      return "text/asp"; 
     case ".pdf": 
      return "application/pdf"; 
     case ".fdf": 
      return "application/vnd.fdf"; 
     case ".ppt": 
      return "application/mspowerpoint"; 
     case ".dwg": 
      return "image/vnd.dwg"; 
     case ".msg": 
      return "application/msoutlook"; 
     case ".xml": 
     case ".sdxl": 
      return "application/xml"; 
     case ".xdp": 
      return "application/vnd.adobe.xdp+xml"; 
     default: 
      return "application/octet-stream"; 
    } 
} 
+0

दुर्भाग्य से मैं भी शुरू में इस कोशिश की थी। मेरे मामले में प्रतिक्रिया के नीचे हटाएं लाइन को स्थानांतरित करते समय। अंत() यह पूरी तरह से हटाएं लाइन को छोड़ देता है, प्रतिक्रिया पर तोड़ता है। अंत()। मैंने पूरी तरह से हटाएं लाइन को हटाने का भी प्रयास किया है, लेकिन अभी भी कोई भाग्य नहीं है। – Lando

2

आपकी समस्या का पालन करने के लिए धन्यवाद। मैंने इस बात का आकलन करने के लिए घंटों बिताए हैं कि इस तथ्य के बावजूद कि कोई भी त्रुटि नहीं हुई थी। बाहर निकलता है यह मेरा AJAX UpdatePanel रहस्यमय और गुप्त रूप से रास्ते में हो रहा था।

0

सर्वर पर राउंड ट्रिप के बिना क्लाइंट साइड (केवल क्रोम) पर टेक्स्ट सहेजने के लिए this को भी आजमाएं।

Here एक और फ्लैश आधार एक ...

1

मैं अपने खोज में इस पोस्ट भर में ठोकर खाई, और पाया है कि यह हमें बता में उपयोगी क्यों UpdatePanel पहली जगह में समस्या के कारण नहीं था।

अद्यतन पैनेल एक असीमित पोस्टबैक है, और Response.TransmitFile को ठीक से काम करने के लिए एक पूर्ण पोस्टबैक की आवश्यकता है।

नियंत्रण है कि चलाता है अतुल्यकालिक पोस्टबैक UpdatePanel में एक ट्रिगर बनाने की ज़रूरत:

<Triggers>   
<asp:PostBackTrigger ControlID="ID_of_your_control_that_causes_postback" /> 
</Triggers> 
संबंधित मुद्दे