开发笔记

.net mvc 从服务器下载文件到本地

2019-08-28  本文已影响0人  孤傲小狼
        /// <summary>
        /// 下载客户端zip文件
        /// </summary>
         public void DownloadClient()
        {
            string url = "D:/InstallationPackage/AlituoclientInstall.zip";
            //string url = Server.MapPath("~/")虚拟路径函数
             DownloadFile(url);
        }
        //下载文件至本地
        public void DownloadFile(string url)
        {
            try
            {
                string strFilePath = url;//服务器文件路径
                FileInfo fileInfo = new FileInfo(strFilePath);
                Response.Clear();
                Response.Charset = "GB2312";
                Response.ContentEncoding = System.Text.Encoding.UTF8;
                Response.AddHeader("Content-Disposition", "attachment;filename=" + Server.UrlEncode(fileInfo.Name));
                Response.AddHeader("Content-Length", fileInfo.Length.ToString());
                Response.ContentType = "application/x-bittorrent";
                Response.WriteFile(fileInfo.FullName);
                Response.End();
            }
            catch (System.Threading.ThreadAbortException ex)
            {
                //不做处理
            }
            catch (Exception ex)
            {
                //做处理
            }
        }
上一篇下一篇

猜你喜欢

热点阅读