C#文件流下載文件

1,推薦指數*****,大文件也可以

string fileName = "test.pdf";
string filePath = Server.MapPath("/" + fileName);
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", "attachment;filename=" + fileName);
Response.TransmitFile(filePath);

 

2,推薦指數****

string fileName = "test.pdf";
string filePath = Server.MapPath("/" + fileName);
FileInfo fileInfo = new FileInfo(filePath);
Response.Clear(); Response.ClearContent(); Response.ClearHeaders(); Response.AddHeader("Content-Disposition", "attachment;filename=" + fileName); Response.AddHeader("Content-Length", fileInfo.Length.ToString());
Response.AddHeader(
"Content-Transfer-Encoding", "binary"); Response.ContentType = "application/octet-stream";//application//pdf Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8"); Response.WriteFile(fileInfo.FullName); Response.Flush(); Response.End();

 

3、推薦指數***,大於400m的文件會卡死

string fileName = "test.pdf";
        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();

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章