Several ways to implement Web site upload and download function by ASP

Source: Internet
Author: User

The functional languages that can be downloaded from the website are dynamic languages, with the exception of ASP. NET, and php,jsp and so on, here is an example of ASP.

This can achieve a variety of Web site download function, the code is as follows:


TransmitFile implementation Download
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/aaa.zip");
Response.TransmitFile (filename);
}

WriteFile Implementation Download
protected void button2_click (object sender, EventArgs e)
{

string filename = "Aaa.zip";//file name saved by the client
String Filepath=server.mappath ("Download/aaa.zip");//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 Sub-block download
protected void Button3_Click (object sender, EventArgs e)
{

string filename = "Aaa.zip";//file name saved by the client
String filePath = Server.MapPath ("Download/aaa.zip");//path

System.IO.FileInfo FileInfo = new System.IO.FileInfo (FilePath);

if (fileinfo.exists = = True)
{
Const LONG ChunkSize = 102400;//100k reads the file each time, only reads 100K, this can alleviate the pressure of the server
byte[] buffer = new Byte[chunksize];

Response.Clear ();
System.IO.FileStream IStream = System.IO.File.OpenRead (FilePath);
Long Datalengthtoread = istream.length;//Gets 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 mode Download
protected void Button4_Click (object sender, EventArgs e)
{
string filename = "Aaa.zip";//file name saved by the client
String filePath = Server.MapPath ("Download/aaa.zip");//path

Download a file as a stream of characters
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";
Notifies the browser to download a file instead of opening it

Response.AddHeader ("Content-disposition", "attachment; Filename= "+ httputility.urlencode (FileName, System.Text.Encoding.UTF8));
Response.BinaryWrite (bytes);
Response.Flush ();
Response.End ();

}

Finished (end)

Several ways to implement Web site upload and download function by ASP

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.