Asp.net download file source code

Source: Internet
Author: User
Add a namespace:
Using system. net;
Using system. IO;

/// <Summary>
/// Download an object
/// </Summary>
/// <Param name = "url"> URL of the file to be downloaded </param>
Public void downloadfile (string URL)
{
WebClient client = new WebClient ();
Int n = URL. lastindexof ('/');
String urladdress = URL. substring (0, n); // obtain the URL
String filename = URL. substring (n + 1, URL. Length-n-1); // get the file name
String dir = server. mappath ("./"); // download file storage path

String Path = dir + '\' + filename; // complete path for storing the downloaded file

Stream stream = client. openread (URL );

Streamreader reader = new streamreader (Stream );
Byte [] Mbyte = new byte [100000];
Int allbyte = (INT) Mbyte. length;
Int startbyte = 0;
While (allbyte> 0) // read cyclically
{
Int M = stream. Read (Mbyte, startbyte, allbyte );
If (M = 0)
Break;

Startbyte + = m;
Allbyte-= m;
}

Filestream fstr = new filestream (path, filemode. openorcreate, fileaccess. Write );
Fstr. Write (Mbyte, 0, startbyte); // write the file
Stream. Close ();
Fstr. Close ();
}
Test passed

Related Article

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.