日期:2014-05-17 浏览次数:20417 次
前台代码: $("#douwn_a").click(function () { $.get('@Url.Action("DownLoadFile", "Internet")?fileName=' + jsondata.routemsg[id - 1].filename + '&&filePath=' + jsondata.routemsg[id - 1].filepath + "&&" + new Date().getTime(), function (json) { }); }); 页面显示: <a id="douwn_a" href="javascript:void(0)">下载</a></div>
后台代码: var newPath = this.Server.MapPath(filePath); //return File(newPath, "application/image/jpeg", fileName); if (!System.IO.File.Exists(newPath)) { throw new ArgumentException("无效的文件名或文件不存在!"); } return new BinaryContentResult() { FileName = fileName, ContentType = "application/image/jpeg", Content = System.IO.File.ReadAllBytes(newPath) }; public class BinaryContentResult : ActionResult { public BinaryContentResult() { } // Properties for encapsulating http headers. public string ContentType { get; set; } public string FileName { get; set; } public byte[] Content { get; set; } // The code sets the http headers and outputs the file content. public override void ExecuteResult(ControllerContext context) { context.HttpContext.Response.ClearContent(); context.HttpContext.Response.ContentType = ContentType; context.HttpContext.Response.AddHeader("content-disposition", "attachment; filename=" + FileName); context.HttpContext.Response.BinaryWrite(Content); context.HttpContext.Response.End(); } }