如何使用Web服务获取pdf并在其上显示html [英] How to get the pdf using web services and show on it html
问题描述
我已将所有准备好的pdf文件放在一个文件夹中我希望使用Web服务获取此pdf文件并使用java脚本在其上显示html
什么我试过了:
var dataString = JSON.stringify({
// accountID:'309'
});
$ .ajax({
类型:GET ,
contentType:application / json,
url:http:// localhost:61185 / WebService.asmx / DownloadFile,
data:dataString,
processData:true,
dataType:json,
成功:函数(响应){
var reader = new FileReader();
reader.readAsArrayBuffer(response.d);
reader.onloadend = function(evt){
console.log(读取成功);
conso le.log(new Uint8Array(evt.target.result));
};
},
错误:函数(a,b ,c){
警报('档案错误')
}
});
[WebMethod()]
[ScriptMethod(UseHttpGet = true,ResponseFormat = ResponseFormat.Json)]
公共字节[] DownloadFile()
{
byte [] filebytes = null;
//使用(FileStream fs = File.OpenRead(APBoard_ReportCard \\APBoard_ReportCard_3844-13_4))
string pdffile = Server.MapPath(〜/ APBoard_ReportCard / APBoard_ReportCard_3844-13_4.pdf);
FileStream fs = new FileStream(pdffile,FileMode.Open,FileAccess.Read);
// {
filebytes = new byte [fs.Length];
fs.Read(filebytes,(int)0,(int)fs.Length);
DataContractJsonSerializer serializer = new DataContractJsonSerializer(filebytes.GetType());
MemoryStream ms = new MemoryStream();
serializer.WriteObject(ms,filebytes);
byte [] jsonString = Encoding.Default.GetBytes( ms.ToString());
ms.Close();
返回jsonString;
//}
//返回filebytes;
//返回jsonString;
}
i have all ready pdf file in a folder i want to get this pdf file using web services and show on it html using java script
What I have tried:
var dataString = JSON.stringify({
//accountID: '309'
});
$.ajax({
type: "GET",
contentType: "application/json ",
url: "http://localhost:61185/WebService.asmx/DownloadFile",
data: dataString,
processData: true,
dataType: "json",
success: function (response) {
var reader = new FileReader();
reader.readAsArrayBuffer(response.d);
reader.onloadend = function (evt) {
console.log("read success");
console.log(new Uint8Array(evt.target.result));
};
},
error: function (a, b, c) {
alert('file error')
}
});
[WebMethod()]
[ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
public byte[] DownloadFile()
{
byte[] filebytes = null;
// using (FileStream fs = File.OpenRead("APBoard_ReportCard\\APBoard_ReportCard_3844-13_4"))
string pdffile = Server.MapPath("~/APBoard_ReportCard/APBoard_ReportCard_3844-13_4.pdf");
FileStream fs=new FileStream(pdffile, FileMode.Open, FileAccess.Read);
// {
filebytes = new byte[fs.Length];
fs.Read(filebytes, (int)0, (int)fs.Length);
DataContractJsonSerializer serializer = new DataContractJsonSerializer(filebytes.GetType());
MemoryStream ms = new MemoryStream();
serializer.WriteObject(ms, filebytes);
byte[] jsonString =Encoding.Default.GetBytes(ms.ToString());
ms.Close();
return jsonString;
// }
//return filebytes;
//return jsonString;
}
推荐答案
.ajax({
类型:GET,
contentType:application / json,
url:http:// localhost:61185 / WebService。 asmx / DownloadFile,
数据:dataString,
processData:true,
dataType:json,
成功:功能(响应){
var reader = new FileReader();
reader.readAsArrayBuffer(response.d);
reader.onloadend = function(evt){
console.log(读取成功);
console.log(new Uint8Array(evt.target.result));
};
},
错误:函数(a,b,c){
alert('文件错误')
}
});
[WebMethod()]
[ScriptMethod(UseHttpGet = true,ResponseFormat = ResponseFormat.Json)]
public byte [] DownloadFile()
{
byte [] filebytes = null;
//使用(FileStream fs = File.OpenRead(APBoard_ReportCard \\APBoard_ReportCard_3844-13_4))
string pdffile = Server.MapPath( 〜/ APBoard_ReportCard / APBoard_ReportCard_3844-13_ 4.pdf);
FileStream fs = new FileStream(pdffile,FileMode.Open,FileAccess.Read);
// {
filebytes = new byte [fs.Length];
fs.Read(filebytes,(int)0,(int)fs.Length);
DataContractJsonSerializer serializer = new DataContractJsonSerializer(filebytes.GetType());
MemoryStream ms = new MemoryStream();
serializer.WriteObject(ms,filebytes);
byte [] jsonString = Encoding.Default.GetBytes( ms.ToString());
ms.Close();
返回jsonString;
//}
//返回filebytes;
//返回jsonString;
}
.ajax({
type: "GET",
contentType: "application/json ",
url: "http://localhost:61185/WebService.asmx/DownloadFile",
data: dataString,
processData: true,
dataType: "json",
success: function (response) {
var reader = new FileReader();
reader.readAsArrayBuffer(response.d);
reader.onloadend = function (evt) {
console.log("read success");
console.log(new Uint8Array(evt.target.result));
};
},
error: function (a, b, c) {
alert('file error')
}
});
[WebMethod()]
[ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
public byte[] DownloadFile()
{
byte[] filebytes = null;
// using (FileStream fs = File.OpenRead("APBoard_ReportCard\\APBoard_ReportCard_3844-13_4"))
string pdffile = Server.MapPath("~/APBoard_ReportCard/APBoard_ReportCard_3844-13_4.pdf");
FileStream fs=new FileStream(pdffile, FileMode.Open, FileAccess.Read);
// {
filebytes = new byte[fs.Length];
fs.Read(filebytes, (int)0, (int)fs.Length);
DataContractJsonSerializer serializer = new DataContractJsonSerializer(filebytes.GetType());
MemoryStream ms = new MemoryStream();
serializer.WriteObject(ms, filebytes);
byte[] jsonString =Encoding.Default.GetBytes(ms.ToString());
ms.Close();
return jsonString;
// }
//return filebytes;
//return jsonString;
}
这篇关于如何使用Web服务获取pdf并在其上显示html的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!