當我點擊客戶端的按鈕時,我想使用AJAX在服務器端調用公共靜態webmethod。靜態方法將創建適當的文件。文件創建後,我需要將其下載到客戶端桌面。我發現了John Culvinar's jquery filedownload plugin,但迄今尚未實現。我知道使用這個插件還需要編寫一個cookie,以便它知道下載完成。我在哪裏把這個代碼放在服務器端?在創建文件後?我很高興如果有人能給我一個樣本在這種情況下,也許在jsfiddle.net在服務器中創建後使用jQuery下載文件
4
A
回答
2
我建議有一個隱藏的iframe更換您的Ajax請求,那麼當你的服務器返回所述文件,它會自動詢問用戶下載。
//name of iframe
var strName = ("uploader" + (new Date()).getTime());
// the iframe
var jFrame = $("<iframe name=\"" + strName + "\" src=\"about:blank\" />").css("display", "none");
jFrame.load(function(objEvent){
// at this point the user should have been asked to download a file.
// Remove the iFrame from the document.
// Because FireFox has some issues with
// "Infinite thinking", let's put a small
// delay on the frame removal.
setTimeout(function(){
jFrame.remove();
},100);
});
var form = $('<form>').attr("action", "upload_act.cfm")
.attr("method", "post")
.attr("enctype", "multipart/form-data")
.attr("encoding", "multipart/form-data")
.attr("target", strName);
form.append('<input type="hidden" name="somename">').val("someval");
$("body:first").append(jFrame, form);
(上面的代碼是原始改編自http://www.bennadel.com/blog/1244-ColdFusion-jQuery-And-AJAX-File-Upload-Demo.htm)
另一種方法是,使之成爲兩個步驟過程。第1步生成文件並返回一個url,第2步用戶單擊下載(這將是指向所述url的錨標記)。
1
如果你想使用jquery插件來增強用戶體驗,你不能從服務器啓動下載。在這種情況下,最好的辦法是在服務器上生成文件,並讓該方法返回文件的路徑。然後只需使用插件。
例子:
$('#btnID').click(function(){
$.ajax({
type: "POST",
url: "/your_webmethod_url",
data: "{'webmethodParam1':'val1','webmethodParam2':'val2'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: fileGenerated,
error: fileNotGenerated
});
});
function fileGenerated(data, textStatus, jqXHR){
//this is the success callback method. start download automatically using the plugin
$.fileDownload(data.d); //returned data from webmethod goes in data.d
}
function fileNotGenerated(jqXHR, textStatus, errorThrown){
//this is the error callback method. do something to handle the error
alert(errorThrown);
}
相關問題
- 1. 創建下載鏈接到文件服務器上的文件
- 2. CSV文件創建和下載不能在服務器上使用PHP
- 3. 在服務器中創建php.ini文件
- 4. 在Azure中創建文件服務器
- 5. 使用jQuery從服務器下載二進制文件
- 6. 如何使用Jquery和Spring Controller從服務器下載文件?
- 7. 從linux服務器下載文件一旦創建
- 8. 無法創建鏈接從服務器下載文件
- 9. 在客戶端 - 服務器系統中創建並下載文本文件
- 10. 使用wcf服務從服務器下載文件
- 11. 將PHP輸出到文件下載而不在服務器上創建文件
- 12. 如何在Android中使用http從服務器下載文件
- 13. 使用PHP下載PDF文件在服務器中被破壞
- 14. 我如何使用戶能夠下載服務器上創建的excel文件
- 15. 如何使用Flex下載動態創建的服務器端文件?
- 16. 用其他服務器下載文件
- 17. 如何在java中使用soap服務器創建wsdl文件
- 18. 使用SimpleXML和JQuery Ajax在服務器中創建一個XML文件
- 19. 無法下載遠程文件在服務器使用睡眠後在php
- 20. 在TFTP服務器上創建文件
- 21. 從Python中的ftp服務器的文件列表中下載最後創建的o修改文件
- 22. 使用spring RestFul服務下載文件
- 23. 使用WCF Rest服務下載文件?
- 24. 使用Restful服務下載zip文件
- 25. 下載文件,使用jQuery後
- 26. 如何創建一個.BAT文件從HTTP \ ftp服務器下載文件?
- 27. 如何在文件下載後斷開FTP服務器連接?
- 28. 使用Renci.ShNet從服務器目錄中下載每個文件
- 29. 使用javascript下載服務器中的文件
- 30. 從asp.net服務器使用Android中的ksoap2下載大文件
如果你可以通過一個URL創建文件(可能是每個會話)來訪問該文件,並且將content-disposition設置爲附件,那麼只需使用'window.location = path_to_file'就行了。 –
我建議用隱藏的iframe替換您的ajax請求,然後當您的服務器返回所述文件時,它會自動要求用戶下載它。另一種方法是將其分爲兩個步驟。第1步生成文件並返回一個url,第2步用戶單擊下載(這將是指向所述url的錨標記)。 –
@KevinB,你可以請你的評論作爲答案,並給我一個示例代碼做這個? –