मुझे एक समान आवश्यकता थी, लेकिन यह भी एक नई विंडो में डाउनलोड करना चाहता था।
मैंने फ़ाइलों की एक सूची डाउनलोड करने के लिए एक जेएस बनाया, और वास्तविक फ़ाइल की बचत करने के लिए एक 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 इस्तेमाल किया।
क्या केवल एक खुला सेव प्रॉम्प्ट होगा। –