使用webservice下载pdf [英] Download pdf using webservice
本文介绍了使用webservice下载pdf的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有标签,它有下载pdf的路径,点击事件调用jquery函数,它本身调用web服务并发送路径作为参数。
在Chrome中我想要强制下载pdf文件。任何人都知道JS中的程序也会起作用。
例如:
function UpdateDownloadCount(DocId) {
debugger;
var ID = document.getElementById(DocId);
var path = ID.getAttribute("name");
jQuery.ajax({
type: "POST", //Example:"POST"
url: "LibraryWebService.asmx/GETPDF", //Example:'WebService.asmx/GetTopicList'
async: false, //Example:"false
data: '{"path": "' + path + '"}', //Example:'{"TopicId": "' + 145 + '"}'
contentType: "application/json",
dataType: "json .pdf",
success: SuccessFunction,
error: ErrorFun
});
}
<a id='" + objUserDocumentlist[i].RefDocumentID + "' önClick='UpdateDownloadCount(this.id)' class='fileDownloadSimpleRichExperience' href='" + filepath + "' target='_blank'>Text</a>
推荐答案
你不能使用javascript或jQuery下载pdf文件(基本上是一个byte [])。您无法从javascript将字节数组保存到文件中。
请看下面的链接:
http://stackoverflow.com/questions/7406581/download-files-using-javascript-and-webservices [ ^ ]
http://stackoverflow.com/questions/2616491 / download-file-using-ajax-and-webservice [ ^ ]
您需要从ASPX页面或ASHX处理程序调用此Web服务;并将回复写回客户。
您可以阅读此主题开头:
http://stackoverflow.com/questions/2239623/download-file -from-webservice-in-asp-net-site [ ^ ]
希望有所帮助!
You can not download the pdf document (basically a byte[]) using javascript or jQuery for that matter. You can't save a byte array to a file from javascript.
Look at the links below:
http://stackoverflow.com/questions/7406581/download-files-using-javascript-and-webservices[^]
http://stackoverflow.com/questions/2616491/download-file-using-ajax-and-webservice[^]
You need to call this web service from a ASPX page or ASHX handler; and than write the response back to the client.
You can read this thread to start with:
http://stackoverflow.com/questions/2239623/download-file-from-webservice-in-asp-net-site[^]
Hope that helps!
I解决了我的问题:
I got Solution to my question:
function SaveToDisk(fileURL, fileName) {
// for non-IE
if (!window.ActiveXObject) {
var save = document.createElement('a');
save.href = fileURL;
save.target = '_blank';
save.download = fileName || 'unknown';
var event = document.createEvent('Event');
event.initEvent('click', true, true);
save.dispatchEvent(event);
(window.URL || window.webkitURL).revokeObjectURL(save.href);
}
// for IE
else if ( !! window.ActiveXObject && document.execCommand) {
var _window = window.open(fileURL, '_blank');
_window.document.close();
_window.document.execCommand('SaveAs', true, fileName || fileURL)
_window.close();
}
}
这篇关于使用webservice下载pdf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文