<a onclick='DownFile(this,\"{1}\")' href='javascript:void(0);' >{0}</a>
function DownFile(my, id) {
var url = '<%=Url.Action("Download") %>' + '/' + id; window.location.href = url; }
[Authorize]
public ActionResult Download(string id) { if (string.IsNullOrEmpty(id)) throw new ArgumentNullException("FileName"); Contract.Domain.Document doc = dsvc.FindById(id);
string physicFolder = Utility.Util.GetPhysicalFolder(doc.SaveFolder);
if (string.IsNullOrEmpty(physicFolder)) throw new ArgumentNullException(doc.SaveFolder, "配置文件未定义该路径!"); using (new Utility.Support.UncAccess(physicFolder))
{ string path = Path.Combine(physicFolder, doc.FileName); var result = global::System.IO.File.OpenRead(path); string fileName = doc.Title + doc.FileExt; HttpContext.Response.Clear(); HttpContext.Response.ContentType = "application/octet-stream"; HttpContext.Response.AddHeader("Content-Disposition", "attachment;filename=\"" + HttpUtility.UrlEncode(fileName) + "\""); //HttpUtility.UrlEncode(Path.GetFileName(path).Trim()) HttpContext.Response.Flush(); HttpContext.Response.WriteFile(path); HttpContext.Response.End(); return View(); //return File(result, "application/octet-stream", doc.Title + doc.FileExt); } } |
|