这是我的Java脚本代码,用于在按钮单击时从数据库下载文件,当按钮单击时此函数调用。使用ajax调用我已经移到处理程序。
function DownloadDocument() {
var CurrentUserEmpId = CurrentSelectedUser;
Ext.Ajax.request({
url: "UploadAttachment.ashx?mode=DownloadDocument&EmployeeId=" + CurrentUserEmpId,
success: function (response) {
var data = response.responseText;
},
failure: function (form, action) {
}
});
}
处理程序页面到了,我已经将文件的字节保存到byte [] buffer。这里的问题是下载无法正常工作。由于Iam是初学者,所以我无法弄清楚问题所在。请对此提供帮助,谢谢。
case "DownloadDocument":
WebClient web = new WebClient();
try
{
byte[] buffer;
var query2 = @"select LLD_Decleration_doc from (select instance, Employee_id, lld_Decleration_doc, ROW_NUMBER() OVER(PARTITION BY Employee_id ORDER BY Update_Date DESC) Latest from [EManager].[dbo].[tax_benefit_declaration]) a where latest = 1 And Employee_id = @EmployeeId";
using (SqlConnection con = new SqlConnection(db.ConnectionString))
using (SqlCommand cmd = new SqlCommand(query2, con))
{
SqlParameter param = cmd.Parameters.Add("@EmployeeId", SqlDbType.Int);
param.Value = EmployeeId;
con.Open();
buffer = (byte[])cmd.ExecuteScalar();
con.Close();
}
HttpResponse response = HttpContext.Current.Response;
response.Clear();
response.ClearContent();
response.ClearHeaders();
response.Buffer = true;
response.ContentType = "APPLICATION/OCTET-STREAM";
String Header = "Attachment; Filename=NewFile";
response.AppendHeader("Content-Disposition", Header);
context.Response.BinaryWrite(buffer);
response.End();
}
catch { }
break;
}
这是很多次说过的话。您不能通过Ajax调用来做到这一点。
您可以通过调用隐藏来实现这一点iframe
,例如:
var body = Ext.getBody();
var comp = body.getById('hiddenform-iframe-download');
if (!Ext.isEmpty(comp)) {
comp.remove();
}
body.createChild({
tag: 'iframe',
cls: 'x-hidden',
id: 'hiddenform-iframe-download',
name: 'iframe',
src: "yourContextToDownload?param1="+something
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句