MVC做一键下载功能,点了按钮没反应,求指点出错原因
点了DownLoadButton之后浏览器卡了一小阵子,断点跟踪二进制流没问题但没有下载对话框弹出来,完全没反应了
我感觉是前段脚本的问题
$(".DownLoadButton").live("click", function () {
$.post("/caseindex/AjaxDownList", {"caseid": "<%=ViewData["CaseId"] %>"});
});
--------------------编程问答-------------------- 下载直接用MVC内置的File方法好了:
[HttpPost]
public ActionResult AjaxDownList(string caseid)
{
MemoryStream ms = new MemoryStream();
byte[] buffer = null;
using (ZipFile file = ZipFile.Create(ms))
{
file.BeginUpdate();
file.NameTransform = new MyNameTransfom();//通过这个名称格式化器,可以将里面的文件名进行一些处理。默认情况下,会自动根据文件的路径在zip中创建有关的文件夹。
//查询需要一键下载的所有文件
FiatDBOperation FDB = new FiatDBOperation();
DataTable dt = FDB.SelectCaseid(caseid);
for (int i = 0; i < dt.Rows.Count; i++)
{
file.Add(Server.MapPath(dt.Rows[i]["FILEPATH"].ToString()));
}
file.CommitUpdate();
buffer = new byte[ms.Length];
ms.Position = 0;
ms.Read(buffer, 0, buffer.Length);
ms.Close();
}
Response.Charset = "UTF-8";
Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8");
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", "attachment;filename=Test.zip");
Response.BinaryWrite(buffer);
Response.Flush();
Response.End();
return new EmptyResult();
}
public class MyNameTransfom : ICSharpCode.SharpZipLib.Core.INameTransform
{
#region INameTransform 成员
public string TransformDirectory(string name)
{
return null;
}
public string TransformFile(string name)
{
return Path.GetFileName(name);
}
#endregion
}
var name = Path.GetFileName(path);
return File(path, "application/x-zip-compressed", Url.Encode(name));
补充:.NET技术 , ASP.NET