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