接收不到
一个script文件 一个ashx 功能是下载文件
script文件
$("#btnDownLoad").click(function () {
$("#form1").ajaxSubmit({
url: "Handler4.ashx",
type: "post",
data:{"title":"downLoad"},
resetForm: "true",
beforSubmit: function () {
},
success: function (msg) {
},
error: function (jqxhr, errorMsg, errorThrown) {
}
})
})
ashx
string title=context.Request.Form["title"];
if (title == "downLoad")
{
string fileName = "downLoadFile.txt";
string filePath = context.Server.MapPath("~/");
FileInfo fileinfo = new FileInfo(filePath);
if (fileinfo.Exists == true)
{
const long size = 102400;
byte[] buffer = new byte[size];
context.Response.Clear();
FileStream fileStream = File.OpenRead(filePath);
long fileLength = fileStream.Length;
context.Response.ContentType = "application/octet-stream";
context.Response.AddHeader("Content-Disposition","attachment;filename="+HttpUtility.UrlEncode(fileName));
//如果客户端连在服务器上
while (fileLength > 0 && context.Response.IsClientConnected)
{
int lengthRead = fileStream.Read(buffer, 0, Convert.ToInt32(size));
context.Response.OutputStream.Write(buffer,0,lengthRead);
context.Response.Flush();
fileLength = fileLength - lengthRead;
}
context.Response.Close();
}
}