FileStream下载文件(物理路径)

//以字符流的形式下载文件
                        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();
View Code

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;
        }
View Code
收藏
关注
评论
原文地址:https://www.cnblogs.com/yidengbone/p/7463237.html