我已經在JavaScript中創建此功能,發送iframe廣告服務器:
function download(url, datas){
if(url && datas){
var inputs = '', value,
iframe = '<iframe name="iframeDownload" id="iframeDownload" width=0 height=0></iframe>';
$(iframe).appendTo('body');
datas.forEach(function(data){
name = encodeURI(data.get('name'));
value = encodeURI(data.get('value'));
inputs+='<input name="'+name+'" value="'+value+'"/>';
});
$('<form action="'+url+'" method="post" target="iframeDownload">'+inputs+'</form>').appendTo('body').submit().remove(); // .appendTo and remove() are needed for firefox
$(iframe).remove();
};
};
我編碼輸入名稱和值能夠發送數據。 在我的服務器上,我使用的是PHP,所以要解碼這個,你需要:rawurldecode。如果你定義的輸入爲「文件名」,並命名爲「文件」,你可以這樣寫:
$fileName = rawurldecode($_POST['fileName']);
$file = rawurldecode($_POST['file']);
比後,強制下載,您需要發送的校正頭。我使用這個功能:
function download($filename, $file) {
header('Content-disposition: attachment; filename="'.$filename.'"');
header('Content-Type: application/force-download');
header('Content-Length: '. filesize($file));
readfile($file);
}
如果你不需要,因爲它在服務器端創建從JavaScript發送的文件,只是你的文件的路徑添加到下載功能。
如果您使用的是PHP,您可以使用fpdf來生成pdf。