. NET download File methods

Source: Internet
Author: User
Tags urlencode

1. Download the file stream
Byte[] filestr=new  byte[5];            MemoryStream Btms = new MemoryStream (FILESTR);            Download the file as a stream of characters             byte[] bytes = new byte[(int) btms.length];            Btms.read (bytes, 0, bytes. Length);            Btms.close ();            Page.Response.ContentType = "Application/octet-stream";            Notifies the browser to download the file instead of opening             Page.Response.AddHeader ("content-disposition", "attachment; Filename= "+ httputility.urlencode (filename, System.Text.Encoding.UTF8));            Page.Response.BinaryWrite (bytes);            Page.Response.Flush ();            Page.Response.End ();

2. Download according to the file path

public void Download (string path)        {            string filePath = HttpContext.Current.Server.MapPath (path);/            /path// Download the 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 ();            HttpContext.Current.Response.ContentType = "Application/octet-stream";            HttpContext.Current.Response.AddHeader ("Content-disposition", "attachment; Filename= "+ httputility.urlencode (" Test ", System.Text.Encoding.UTF8));            HttpContext.Current.Response.BinaryWrite (bytes);            HttpContext.Current.Response.Flush ();            HttpContext.Current.Response.End ();        }

3. Direct download

WebClient client = new WebClient ();

File storage Path
var path = HttpContext.Current.Server.MapPath (@ "\static\btdownload\");

Determine if a file exists
var file = new FileInfo (path + filename);
if (!file. Exists)
{
Client. DownloadFile (value, path + filename);
}

. NET download File methods

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.