在MVC3.0的一个CSHTML中一个<a>标签,需要点击<a>标签,来下载一个文件(包括可以下载图片,txt文件),为什么我的没效果,弹不出那个保存,打开,取消的提示框,求高手指点。
- C# code
前台代码: $("#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>
- C# code
后台代码: 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(); } }
------解决方案--------------------------------------------------------
如果是要让客户端下载你服务器上的文件
<a id="douwn_a" href="http://xxxx.com/upload/123.jpg">下载</a> 就OK了么。
如果文件存在数据库,只需要替换href,值就行了.
------解决方案--------------------------------------------------------
楼主 给力点