2014-09-26 51 views
2

我創建了一個wordpress頁面,允許用戶在5秒後下載zip文件。其中,我打電話給第二個頁面並傳遞POST參數(zip id ::以獲取zip路徑)。該頁面被調用,但始終是彈出式窗口。我在尋找一個乾淨的下載選項,其中無需打開標籤或新窗口,從開始下載。旁遮普彈出 - 阻滯劑也。下載文件時禁用彈出窗口打開

我曾嘗試兩種方法

A)方法1(Jquery的郵政)

$.post(
     "<?php echo get_permalink(get_page_by_title('Download Page')); ?>", 
     {attachment_path: "<?php echo $attachment[0]->ID ; ?>" 
    } 

B)方法2(提交表格)

$('<form action="" method="post" target="_parent" class="hide"> 
     <input type="hidden" name="attachment_path" value="<?php echo $attachment[0]->ID ; ?>" /> 
     <input type="submit" name="submit" value="submit" id="target-counter-button"/> 
    </form>' 
    ).submit(); 

編輯

1)尋找一個POST方法來實現邏輯

2)由於服務器的限制,直接進入* .php文件和* .zip文件已被封鎖

3)家長下載頁面應該在這個過程中

尋找一個專家的意見保持開放在這個問題上。

感謝

+0

頭正好被設置爲正確的類型,並調用URL – Luke 2014-09-26 15:31:08

+0

它的一個客戶要求先打開下載頁面,然後開始下載文件後的5秒 – 2014-09-26 15:37:51

+0

只要做到這一點,而無需打開一個新的窗口,然後...... – Luke 2014-09-26 15:38:42

回答

1

您可以通過執行重定向到PHP腳本的路徑實現這個怎麼會以正確的HTTP頭輸出端所需的文件。

JavaScript部分:

重定向後的5秒到了 'zipfetcher' 腳本與拉鍊ID

<script> 
function download(id) { 
    setTimeout(function() { 
     document.location = "zipfetcher.php?fileid="+id; // The path of the file to download 
    }, 5000); // Wait Time in milli seconds 
} 
</script> 

<a href="#" onclick="download('123');return false;">Click to download in 5 sec!</a> // Call of the download function when clicked 

PHP部分:(又名zipfetcher.php)

查找根據zipid的zip路徑然後使用readfile輸出到具有正確標題的瀏覽器

// Fetch the file path according to $_GET['fileid'] who's been sent by the download javascript function etc. 
// .... 

header('Content-Description: File Transfer'); 
header('Content-Type: application/zip, application/octet-stream'); // Puting the right content type in this case application/zip, application/octet-stream 
header('Content-Disposition: attachment; filename='.basename($filepath)); 
header('Expires: 0'); 
header('Cache-Control: must-revalidate'); 
header('Pragma: public'); 
header('Content-Length: ' . filesize($filepath)); 
readfile($filepath); 
exit; 
+0

無法允許直接鏈接到前端,其腳本作業可以獲取並下載它。 – 2014-09-26 15:36:17

+0

但是,我想我們在這裏也有交互窗口,要求保存或取消??我是禮@isaac – maddy 2014-09-26 15:36:41

+0

@RahulGupta我剛剛更新了我的答案,告訴你如何在隱藏完整路徑的情況下如何使用PHP腳本和Javascript來做到這一點,我只是在我的電腦上測試它像魅力一樣工作;) – Isaac 2014-09-26 15:50:08

相關問題