首页 > 其他 > 详细

文件下载

时间:2014-04-16 16:47:49      阅读:516      评论:0      收藏:0      [点我收藏+]

服务端数据处理     

  public ActionResult FileDownload(int fileId)
        {
            var file = FileBLL.GetFileInfoById(fileId);
            using (var fStream = new FileStream(file.FileLocation, FileMode.Open))
            {
                fStream.Seek(0, SeekOrigin.Begin);
                var content = new byte[fStream.Length];
                fStream.Read(content, 0, (int)fStream.Length);
                fStream.Close();
                return new BinaryContentResult("application/octet-stream", file.FileName, content);
            }
        }

 

 

    public class BinaryContentResult:ActionResult
    {
        public string Browser { get; set; }
        public string ContentType { get; set; }
        public string FileName { get; set; }
        public byte[] Content { get; set; }

        public BinaryContentResult()
        {}

 

文件传输
        public BinaryContentResult(string contentType, string fileName, byte[] content)
        {
            ContentType = contentType;
            FileName = fileName;
            Content = content;
        }

        public override void ExecuteResult(ControllerContext context)
        {
            var browser=context.HttpContext.Request.Browser.Browser;
            if (browser.ToLower().Contains("ie"))
            {
                var ext = FileName.Substring(FileName.LastIndexOf(‘.‘));
                var name = FileName.Remove(FileName.Length - ext.Length);
                name = name.Replace(".", "%2e");
                FileName = name + ext;
            }

            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();
        }
    }

 

 

文件下载,布布扣,bubuko.com

文件下载

原文:http://www.cnblogs.com/yonglong/p/3666806.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!