分享

Web服务器上文件下载

 若生安饶 2012-08-28
<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);
            }
        }
 

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多