我查看了许多在线资源并构建了我的脚本,但它似乎仍然没有给我文件。它只是加载页面“client-home2.aspx/downloadAttachment”,而不是执行代码并交付文件。
这是我背后的代码:
[WebMethod]
public void downloadAttachment(string id)
{
DbProviderFactory dbf = DbProviderFactories.GetFactory();
using (IDbConnection con = dbf.CreateConnection())
{
string sSQL;
sSQL = "select top 1 " + ControlChars.CrLf
+ " FILENAME, FILE_MIME_TYPE, ATTACHMENT" + ControlChars.CrLf
+ " from vwATTACHMENTS_CONTENT" + ControlChars.CrLf
+ " where 1 = 1 " + ControlChars.CrLf;
//Debug.Print(sSQL);
using (IDbCommand cmd = con.CreateCommand())
{
cmd.CommandText = sSQL;
Sql.AppendParameter(cmd, id.ToString(), "ATTACHMENT_ID");
cmd.CommandText += " order by DATE_ENTERED desc" + ControlChars.CrLf;
using (DbDataAdapter da = dbf.CreateDataAdapter())
{
((IDbDataAdapter)da).SelectCommand = cmd;
using (DataTable dt = new DataTable())
{
da.Fill(dt);
if (dt.Rows.Count > 0)
{
foreach (DataRow r in dt.Rows)
{
string name = (string)r["FILENAME"];
string contentType = (string)r["FILE_MIME_TYPE"];
Byte[] data = (Byte[])r["ATTACHMENT"];
// Send the file to the browser
Response.AddHeader("Content-type", contentType);
Response.AddHeader("Content-Disposition", "attachment; filename=" + name);
Response.BinaryWrite(data);
Response.Flush();
Response.End();
}
}
else
{
}
}
}
}
}
}
这是我的 jQuery:
$('.attachmentLink').click(function () {
var id = $(this).attr('id');
/*
$.ajax({
type: "POST",
url: "client-home2.aspx/downloadAttachment",
data: '{id:\'' + id + '\'}',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
//alert(msg.d);
}
});
*/
$.download('client-home2.aspx/downloadAttachment', 'id=' + id);
return false;
});
我正在使用这个功能http://filamentgroup.com/lab/jquery_plugin_for_requesting_ajax_like_file_downloads/
问题是,它从来没有给我文件;它只是导航到client-home2.aspx/downloadAttachment
.
如何解决此问题以便我可以下载文件?
谢谢你。