[英]How to call a function in ajax from web service that contains pdf attachment?
In my web services i can able to download the file , but i don,t know how to call this function in ajax. 在我的Web服务中,我可以下载文件,但是我不知道如何在Ajax中调用此函数。
I have datatable that contains button column, i want to achieve if i click the button in datatable , i want to download a pdf file from sql server database 我有包含按钮列的数据表,我想通过单击数据表中的按钮来实现,我想从sql server数据库下载pdf文件
please refer below my web service [WebMethod] public void DownloadFile(string id) { 请在下面参考我的网络服务[WebMethod] public void DownloadFile(string id){
byte[] bytes;
//var id = HttpContext.Current.Request.Form["id"];//add this line to ref to id
string contentType;
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["const"].ConnectionString);
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = "select Attachment,SalesNumber from invoice where SalesNumber=@Id";
cmd.Parameters.AddWithValue("@Id", id);
cmd.Connection = con;
con.Open();
using (SqlDataReader sdr = cmd.ExecuteReader())
{
sdr.Read();
bytes = (byte[])sdr["Attachment"];
// var bytes = File.ReadAllBytes(@"C:\temp\a.png");
contentType = sdr["SalesNumber"].ToString();
}
con.Close();
}
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Buffer = true;
HttpContext.Current.Response.Charset = "";
HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
HttpContext.Current.Response.ContentType = @"application/pdf";// "image/png";
//HttpContext.Current.Response.BinaryWrite(Buffer);
HttpContext.Current.Response.AddHeader("Content-Length", bytes.Length.ToString());
HttpContext.Current.Response.OutputStream.Write(bytes, 0, bytes.Length);
HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment; filename=" + contentType +".pdf");
HttpContext.Current.Response.End();
HttpContext.Current.Response.Flush();
}
my ajax function for each row button:- 我的每行按钮的ajax函数:-
$('#datat tbody').on('click', 'button', function () { $('#datat tbody')。on('click','button',function(){
var currow = $(this).closest('tr');
var col1 = currow.find('td:eq(1)').text();
$.ajax({
url: 'WebService1.asmx/DownloadFile',
data: JSON.stringify({ id: col1}),
method: 'post',
contentType: 'application/json;charset=utf-8',
dataType: 'json',
success: function (data) {
alert('PDF generated successfully.');
}
});
Please advise me a solution ... thanks for your help 请告诉我一个解决方案...谢谢您的帮助
I don't know if this is all you need, or if it will work from a web service, but i think you need at least 我不知道这是否是您所需要的,或者它是否可以通过Web服务运行,但是我认为您至少需要
// note the quotes around the file name.
Response.AppendHeader("Content-Disposition",
"attachment; filename=\"" + fileName + "\"");
and 和
Response.TransmitFile(Server.MapPath("~/pathToYourFiles/" + fileName));
I do this without an output stream, i just configure the response (most of what you already have) and TransmitFile, so this might not work for you. 我在没有输出流的情况下执行此操作,我只配置了响应(大多数已使用的响应)和TransmitFile,所以这可能对您不起作用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.