IE

2011-03-18 34 views
6

में एक शॉट में एकाधिक फ़ाइलों को डाउनलोड करने के लिए कैसे मैं jsp में बटन के क्लिक पर एकाधिक फ़ाइलों को डाउनलोड करना चाहता हूं।
मैं एक सर्वलेट को दो बार कॉल करने के लिए जेएस में निम्न कोड का उपयोग कर रहा हूं।IE

var iframe = document.createElement("iframe"); 
iframe.width = iframe.height = iframe.frameBorder = 0; 
iframe.scrolling = "no"; 
iframe.src = "/xyz.jsp?prodId=p10245"; 
document.getElementById("iframe_holder").appendChild(iframe); 

var iframe2 = document.createElement("iframe"); 
iframe2.width = iframe2.height = iframe2.frameBorder = 0; 
iframe2.scrolling = "no"; 
iframe2.src = "/xyz.jsp?prodId=p10243"; 
document.getElementById("iframe_holder").appendChild(iframe2); 

xyz.jsp में मैं सर्वलेट जो एक पथ से फाइल डाउनलोड करता है और ब्राउज़र पर इसे भेजने के बोल रहा हूँ।
समस्या यह है कि यह सफारी, फ़ायरफ़ॉक्स काम कर रहा है लेकिन आईई में नहीं।
हम आईई के साथ कई फाइलें डाउनलोड नहीं कर सकते हैं?

उत्तर

4

डिज़ाइन द्वारा, गैर-उपयोगकर्ता द्वारा शुरू की गई फ़ाइल डाउनलोड IE में अवरुद्ध हैं। इसका स्वाभाविक अर्थ यह है कि एक उपयोगकर्ता-क्लिक के परिणामस्वरूप एक से अधिक फ़ाइल डाउनलोड करना संभव नहीं होना चाहिए।

4

मैं IE और क्रोम

में एकाधिक फ़ाइलों को डाउनलोड करने के लिए निम्नलिखित कोड का उपयोग किया है
function downloadFile(url) 
{ 
    var iframe = document.createElement("iframe"); 
    iframe.src = url; 
    iframe.style.display = "none"; 
    document.body.appendChild(iframe); 
} 

function downloadFiles(urls) 
{ 
    downloadFile(urls[0]); 
    if (urls.length > 1) 
     window.setTimeout(function() { downloadFiles(urls.slice(1)) }, 1000); 
} 

आप (downloadFiles करने के लिए URL की एक सरणी से पारित) समारोह है, जो एक छोटी के साथ प्रत्येक के लिए DownloadFile() कॉल करेंगे बीच में देरी यह काम करने के लिए देरी की कुंजी लगती है!

+0

क्या केवल एक खुला सेव प्रॉम्प्ट होगा। –

0

मुझे एक समान आवश्यकता थी, लेकिन यह भी एक नई विंडो में डाउनलोड करना चाहता था।

मैंने फ़ाइलों की एक सूची डाउनलोड करने के लिए एक जेएस बनाया, और वास्तविक फ़ाइल की बचत करने के लिए एक PHP। मैंने ऊपर एक प्रारंभिक बिंदु के रूप में उपयोग किया, और PHP से शुरू होता है (ठीक है, मूल स्रोत नहीं मिल सकता है)। मैं उत्तीर्ण यूआरआई को एन्कोड करता हूं इसलिए फ़ाइल नामों में रिक्त स्थान परेशानी का कारण नहीं बनते हैं।

(function() { 
"use strict"; 

var files = [],    // Array of filename strings to download 
newWindow,     // New window to handle the download request 
secondsBetweenDownloads;  // Wait time beteen downloads in seconds 
// 
// Download a file using a new window given a URI 
// 
    function downloadFile(uri) { 
    if (!newWindow) { 
     newWindow = window.open('', 
      '', 
      'width=1500 height=100'); 
    } 
    if (newWindow) { 
     newWindow.location = 
      'saveAs.php?' + 
      'file_source=' + encodeURI(uri); 
     newWindow.document.title = "Download File: " + uri; 
    } else { 
     console.log("Unable to open new window. Popups blocked?"); 
    } 
} 
// 
// Download all files specified in the files[] array from siteName. 
// Download the file at array position zero, then set a timeout for 
// secondsBetweenDownloads seconds 
// 
function downloadFiles(siteName) { 
    var showTime = new Date(); 

    console.log(
      showTime.toTimeString().substring(0,8) + 
      " Starting download for: " + files[0] 
     ); 
    // Skip any empty entries, download this file 
    if (files[0].length > 0) downloadFile(siteName + files.splice(0, 1)); 
    if (files.length > 0) {      // If more files in array 
      window.setTimeout(function() { // Then setup for another iteration 
       downloadFiles(siteName); 
      }, secondsBetweenDownloads * 1000); // Delay for n seconds between requests 
    } else { 
     newWindow.close();      // Finished, close the download window 
    } 
} 
// 
// Set the web site name and fill the files[] array with the files to download 
// then kick off the download of the files. 
// 
$(document).ready(function() { 
    var 
    siteName** = "http://www.mysteryshows.com/thank-you/"; 
    secondsBetweenDownloads** = 35; // N seconds delay between requests 

    files = [ 
     "show1.mp3", 
     "show2.mp3" 
    ]; 
    downloadFiles(siteName, files); 
}); 
}()); 

पृष्ठ के लिए HTML सरल है। मूल रूप से कोई वाक्यविन्यास-अनुरूप पृष्ठ करेगा।

saveAs.php पृष्ठ जो js फ़ाइल newWindow.location लाइन में उपयोग करता है केवल php है।

 <?php 
    if (isset($_GET['file_source'])) { 
     $fullPath = $_GET['file_source']; 
     if($fullPath) { 
      $fsize = filesize($fullPath); 
      $path_parts = pathinfo($fullPath); 
      $ext = strtolower($path_parts["extension"]); 
      switch ($ext) { 
       case "pdf": 
       header("Content-Disposition: attachment; 
       filename=\"".$path_parts["basename"]."\""); // use 'attachment' to 
                  // force a download 
       header("Content-type: application/pdf"); // add here more headers for 
                 // diff. extensions 
       break; 
       default; 
       header("Content-type: **application/octet-stream**"); 
       header("Content-Disposition: 
        filename=\"".$path_parts["basename"]."\""); 
      } 
      if($fsize) {//checking if file size exist 
       header("Content-length: $fsize"); 
      } 
      $request = $path_parts["dirname"] . '/' . 
       rawurlencode($path_parts["basename"]); 
      readfile($request); 
      exit; 
     } 
    } 
?> 

मैं सुनिश्चित करने के लिए यह एक वैध, इनकोडिंग अनुरोध था यूआरआई की सिर्फ 'basename' भाग पर rawurlencode इस्तेमाल किया।

 संबंधित मुद्दे