//以字符流的形式下载文件 FileStream fs = new FileStream(filePath, FileMode.Open); byte[] bytes = new byte[(int)fs.Length]; fs.Read(bytes, 0, bytes.Length); fs.Close(); Response.Charset = "utf-8"; Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8"); Response.ContentType = "application/octet-stream"; if (HttpContext.Current.Request.UserAgent.ToUpper().Contains("MSIE") || HttpContext.Current.Request.UserAgent.ToUpper().Contains("TRIDENT")|| HttpContext.Current.Request.UserAgent.ToUpper().Contains("EDGE")) { fileName = HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8); } else { fileName = fileName.Replace(" ", "_"); } //fileName = HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8); Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName); Response.BinaryWrite(bytes); Response.Flush(); Response.End();
filePath路径
public string GetDownPath(string RecordNum, string Name) { string sPath = Page.Server.MapPath("/"); string filePath = Path.Combine(Path.Combine(sPath, "Upload\\Attachment\\" + RecordNum), Name); return filePath; }