2013年9月12日星期四

C # file download four methods


using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}

//TransmitFile实现下载
protected void Button1_Click(object sender, EventArgs e)
{
Response.ContentType = "application/x-zip-compressed";
Response.AddHeader("Content-Disposition", "attachment;filename=z.zip");
string filename = Server.MapPath("DownLoad/z.zip");
Response.TransmitFile(filename);
}

//WriteFile实现下载
protected void Button2_Click(object sender, EventArgs e)
{
string fileName ="asd.txt";//客户端保存的文件名
string filePath=Server.MapPath("DownLoad/aaa.txt");//路径

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";
Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
Response.WriteFile(fileInfo.FullName);
Response.Flush();
Response.End();
}

//WriteFile分块下载
protected void Button3_Click(object sender, EventArgs e)
{
string fileName = "aaa.txt";//客户端保存的文件名
string filePath = Server.MapPath("DownLoad/aaa.txt");//路径

System.IO.FileInfo fileInfo = new System.IO.FileInfo(filePath);

if (fileInfo.Exists == true)
{
const long ChunkSize = 102400;//100K 每次读取文件,只读取100K,这样可以缓解服务器的压力
byte[] buffer = new byte[ChunkSize];

Response.Clear();
System.IO.FileStream iStream = System.IO.File.OpenRead(filePath);
long dataLengthToRead = iStream.Length;//获取下载的文件总大小
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName));
while (dataLengthToRead > 0 && Response.IsClientConnected)
{
int lengthRead = iStream.Read(buffer, 0, Convert.ToInt32(ChunkSize));//读取的大小
Response.OutputStream.Write(buffer, 0, lengthRead);
Response.Flush();
dataLengthToRead = dataLengthToRead - lengthRead;
}
Response.Close();
}
}

//流方式下载
protected void Button4_Click(object sender, EventArgs e)
{
string fileName = "aaa.txt";//客户端保存的文件名
string filePath = Server.MapPath("DownLoad/aaa.txt");//路径

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

}
}

------ Solution ------------------------------------- -------
< br> ------ Solution ----------------------------------------- ---
Oh, the cattle must be the top ( often see this avatar appear )
------ Solution ------------------- -------------------------
good collection
------ Solution --------- -----------------------------------
collection , thank
----- - Solution --------------------------------------------
what situation ?

------ Solution ------------------------------------ --------

------ Solution ---------------------------------------- ----
look

Response.End (); cause many problems , prompting download box appears, the page can not jump
------ Solution --------------- -----------------------------

export excel. Can only be derived by other methods , such as office excel component to export . There is no other way ? ? ? ? ( Guaranteed to save the download pop-up box )
------ Solution -------------------------------- ------------
handsome code ! ! ! !
------ Solution ---------------------------------------- ----
and , like this post it ? ? ?

C # file download four methods - Shen Wei - blog Park


I only had a title , huh
------ Solution ---------------------------- ----------------

exactly the same. . .
landlord and that Shen Wei is a man? ? ? ?
------ Solution ---------------------------------------- ----
how are the English way ?
------ Solution ---------------------------------------- ----

say what
------ Solution -------------------------- ------------------

say what
------ Solution ------------ --------------------------------
applause
------ Solution --- -----------------------------------------


This is not the same , I made that time too early. You made ​​this time is too late
------ Solution - -------------------------------------------
good thing
------ Solution ------------------------------------------ -
read

------ Solution ------------------------------------ --------

------ Solution ---------------------------------------- ----
cattle top up a little rookie understand
------ Solution ---------------------- ----------------------
collection , thank you ~
------ Solution ---------- ----------------------------------
collection. . . .
------ For reference only -------------------------------------- -
  This reply was moderator deleted at 2012-08-22 11:32:54

------ For reference only ---------------------------------- -----


------ For reference only ---------------------------------------
  This reply was moderator deleted at 2012-08-22 13:38:01

------ For reference only ---------------------------------- -----


http://www.cnblogs.com/Veakey/archive/2012 / 08/17/2644123.html

This is the same kind enough to do
------ For reference only --------------------------- ------------
  This reply was moderator deleted at 2012-08-22 15:51:17

------ For reference only ---------------------------------- -----
  This reply was moderator deleted at 2012-08-22 16:33:01

------ For reference only ---------------------------------- -----
collection , thank you ~
------ For reference only ------------------------- --------------
collection .
------ For reference only -------------------------------------- -
  This reply was moderator deleted at 2012-08-22 17:01:56

------ For reference only ---------------------------------- -----
very powerful learning to landlord
------ For reference only ---------------------- -----------------
  This reply was moderator deleted at 2012-08-22 17:19:37

------ For reference only ---------------------------------- -----
good collection
------ For reference only ---------------------------------------
Thank you ! Collection !
------ For reference only -------------------------------------- -
learning , a collection !
------ For reference only -------------------------------------- -
collection
------ For reference only -------------------------------- -------
  This reply was moderator deleted at 2012-08-23 09:53:42

------ For reference only ---------------------------------- -----
top one, thanks for sharing
------ For reference only ------------------------- --------------
very powerful
------ For reference only ------------------- --------------------
Niu I'd like to learn the C language are still in my state of light water ! ! !
------ For reference only -------------------------------------- -
pro collection .
------ For reference only -------------------------------------- -
  This reply was moderator deleted at 2012-08-23 08:34:36

------ For reference only ---------------------------------- -----
  This reply was moderator deleted at 2012-08-23 09:12:30

------ For reference only ---------------------------------- -----
If you get a file from a disk , or TransmitFile best
------ For reference only ------------------- --------------------
  This reply was moderator deleted at 2012-08-23 09:43:41

------ For reference only ---------------------------------- -----
  This reply was moderator deleted at 2012-08-23 09:43:04

------ For reference only ---------------------------------- -----
four methods well, learn .
------ For reference only -------------------------------------- -
amount did not understand ah.
------ For reference only -------------------------------------- -
  This reply was moderator deleted at 2012-08-23 09:24:58

------ For reference only ---------------------------------- -----
  This reply was moderator deleted at 2012-08-23 08:40:10

------ For reference only ---------------------------------- -----
  This reply was moderator deleted at 2012-08-23 09:25:04

------ For reference only ---------------------------------- -----
not the same download Well , , , , uh
------ For reference only -------- -------------------------------
 Why should web
------ For reference only ----------- ----------------------------
good collection
------ For reference only ---- -----------------------------------
good collection of nice collection
--- --- For reference only ---------------------------------------
good collection the
------ For reference only ------------------------------------- -
hope to use when drawing
------ For reference only -------------------------- -------------
good first collection
------ For reference only ------------------ ---------------------
Thank you to share . . .
------ For reference only -------------------------------------- -
if more documents are recommended : ASP.Net bulk download solutions
----- - For reference only ---------------------------------------

good.
------ For reference only ---------------------------------------
I have nothing to do noble stickers purpose, just an i-type i dad landlord posts dingqilai.
------ For reference only ------------------------ ---------------
  This reply was moderator deleted at 2012-09-13 10:58:07

------ For reference only ---------------------------------- -----
just learning , is not very clear !
------ For reference only -------------------------------------- -
Thank collection ! !
------ For reference only -------------------------------------- -
  This reply was moderator deleted at 2012-08-23 11:31:15

------ For reference only ---------------------------------- -----
Thank you to share !
------ For reference only -------------------------------------- -
collection what
------ For reference only -------------------------------- -------
collection under
------ For reference only -------------------------- -------------

------ For reference only --------------------------------- ------
good collection ! !
------ For reference only -------------------------------------- -
pro collection .
------ For reference only -------------------------------------- -
learn it ~ !
------ For reference only -------------------------------------- -
collection !
------ For reference only -------------------------------------- -
learned last just made ​​a pda wireless upload and download data
network conditions : Connecting wifi or 3G
------ For reference only -------------------------- -------------
good way to learn, thanks for sharing !
------ For reference only -------------------------------------- -
no points , and where to look to
------ For reference only -------------------------- -------------
good things good
------ For reference only ----------------- ----------------------
  This reply was moderator deleted at 2012-08-23 15:08:35

------ For reference only ---------------------------------- -----
  This reply was moderator deleted at 2012-08-23 16:50:50

------ For reference only ---------------------------------- -----
good complex ah do not understand
------ For reference only ---------------------- -----------------
good collection
------ For reference only -------------- -------------------------

Thanks for sharing , collection .
------ For reference only -------------------------------------- -
favorites !
------ For reference only -------------------------------------- -
Thank you for sharing , a collection !
------ For reference only -------------------------------------- -
this nice collection learned
------ For reference only --------------------------- ------------
powerful powerful , collectibles , and learning to learn. .
------ For reference only -------------------------------------- -
Thank you to share .
------ For reference only -------------------------------------- -
HTTP if there would be better
------ For reference only ------------------------ ---------------
admire ah , terrible !
------ For reference only -------------------------------------- -
learned
------ For reference only -------------------------------- -------
collection ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
------ For reference only ----------------- ----------------------
  This reply was moderator deleted at 2012-09-02 10:30:09

------ For reference only ---------------------------------- -----
  This reply was moderator deleted at 2012-09-02 10:31:07

------ For reference only ---------------------------------- -----
study and collection of the
------ For reference only ------------------------- --------------
  This reply was moderator deleted at 2012-08-24 09:29:32

------ For reference only ---------------------------------- -----
Thank collection ! !
------ For reference only -------------------------------------- -
  This reply was moderator deleted at 2012-08-24 09:54:32

------ For reference only ---------------------------------- -----
top about

没有评论:

发表评论