下载
来源:互联网 发布:淘宝店铺层级怎么刷 编辑:程序博客网 时间:2024/04/30 04:13
#region 下载附件 private void downLoadFile(string fileName) { try { fileName = fileName.Split(';')[0]; string path = Server.MapPath(fileName); FileInfo fi = new FileInfo(path); if (fi.Exists) { Response.Clear(); Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(fi.Name)); Response.AddHeader("Content-Length", fi.Length.ToString()); Response.ContentType = "application/octet-stream"; Response.Filter.Close(); Response.WriteFile(fi.FullName); Response.End(); } } catch (Exception ex) { throw ex; } } private void DownLoad(string fileName) { try { fileName = fileName.Split(';')[0]; string filePath = Server.MapPath(fileName); //以字符流的形式下载文件 FileStream fs = new FileStream(filePath, FileMode.Open); byte[] bytes = new byte[(int)fs.Length]; fs.Read(bytes, 0, bytes.Length); fs.Close(); Response.ContentType = "application/octet-stream"; //通知浏览器下载文件而不是打开 Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8)); Response.BinaryWrite(bytes); Response.Flush(); Response.End(); } catch (Exception ex) { throw ex; } } #endregion
0 0