C # ASP. NET download file to local

Source: Internet
Author: User

<summary>
Web download file
</summary>
<param name= "Downloadpath" > Target address </param>
<param name= "Fullfilepath" > Original address </param>
<param name= "filename" > file name </param>
<returns></returns>

public static bool Downloadsoft (string Downloadpath, String Fullfilepath, String FileName)

{
BOOL flag = FALSE;
Try
{
if (! Directory.Exists (Downloadpath))
{
Directory.CreateDirectory (Downloadpath);
}
using (FileStream fs = new FileStream (Downloadpath + "/" + FileName, FileMode.Create))
{
Create request
WebRequest request = WebRequest.Create (Fullfilepath + FileName);
Receive response
WebResponse response = Request. GetResponse ();
Output stream
Stream Responsestream = Response. GetResponseStream ();
byte[] bufferbytes = new byte[10000];//buffer byte array
int bytesread =-1;
while ((Bytesread = Responsestream.read (bufferbytes, 0, bufferbytes.length)) > 0)
{
Fs. Write (bufferbytes, 0, bytesread);
}
if (fs. Length > 0)
{
Flag = true;
}
Turn off write
Fs. Flush ();
Fs. Close ();
}


}
catch (Exception exp)
{
return error message


}
return flag;
}

C # ASP. NET download file to local

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.