Description:
Four Types
ASP. NETFile Download
// Download transmitfile
Protectedvoid
Button#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 );
}
// Download writefile
Protectedvoid button2_click (Object sender, eventargs E)
{
String filename
= "Asd.txt"; // file name saved by the client
String
Filepath = server. mappath ("download/aaa.txt"); // path
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 multipart download
Protectedvoid button3_click (Object sender, eventargs E)
{
String filename
= "Aaa.txt"; // file name saved by the client
String
Filepath = server. mappath ("download/aaa.txt"); // path
System. Io. fileinfo
Fileinfo = Newsystem. Io. fileinfo (filepath );
If
(Fileinfo. exists =
True)
{
Const
Long chunksize =
102400; // 100 K read files each time, only K read, this can ease the pressure on the server
Byte []
Buffer = new byte [chunksize];
Response. Clear ();
System. Io. filestream
Istream
= System. Io. file. openread (filepath );
Long
Datalengthtoread =
Istream. length; // get the total size of the downloaded file
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); // read size
Response. outputstream. Write (buffer,
0,
Lengthread );
Response. Flush ();
Datalengthtoread
= Datalengthtoread-
Lengthread;
}
Response. Close ();
}
}
// Stream download
Protectedvoid button4_click (Object sender, eventargs E)
{
String filename =
"Aaa.txt"; // file name saved by the client
String
Filepath = server. mappath ("download/aaa.txt"); // path
// download an object as a response stream
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";
// notify the browser to download the file instead of opening
response. addheader ("content-disposition", "attachment;
filename =" + httputility. urlencode (filename,
system. text. encoding. utf8);
response. binarywrite (bytes);
response. flush ();
response. end ();
}