Home » ASP.NET » Asp.net file download

Asp.net file download

asp.net file download:


protected, void, down_Click (object, sender, EventArgs, e)
{
String fileName = ".Rar image hover"; / / client to save the file name
String filePath = Server.MapPath ("file/ image hover.Rar"); / / path
In the form of a stream of characters / download files
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"";
Notice / browser to download the file not open
Response.AddHeader ("Content-Disposition", "attachment", "filename=" + HttpUtility.UrlEncode (fileName, System.Text.Encoding.UTF8));
Response.BinaryWrite (bytes);
Response.Flush ();
Response.End ();
}


Asp.net MVC file download:


, public, ActionResult, FileDown ()
{
//byte[] content=System.IO.File.ReadAllBytes (Server.MapPath ("/file/ASP.NET MVC4 development guide.Pdf"));
FtpStream / / Stream = System.IO.File.Open (Server.MapPath ("/file/ASP.NET MVC4.Pdf Development Guide"), FileMode.Open);
//return File (ftpStream, System.Net.Mime.MediaTypeNames.Application.Octet, "MVC4 development guide,.Pdf");
If (... System.IO.File.Exists (Server.MapPath ("/file/sql.txt"))
{
Return JavaScript ("");;
}
VaR, FS, =System.IO.File.Open (Server.MapPath ("/file/sql.txt"), FileMode.Open);
Var bytes = new, byte[(int) fs.Length];
Fs.Read (bytes, 0, bytes.Length);
Fs.Close ();
Return File (bytes, "application/octet-stream", "SQL statement.Txt");
}


Asp.net MVC background returns picture:


, public, ActionResult, Index ()
{
//Uri url = Request.UrlReferrer;
Byte[] data = System.IO.File.ReadAllBytes (Server.MapPath ("/Images/heroAccent.png"));
//return File (data, @ image/jpeg ',' heroAccent.png ');
Return File (data, @ image/jpeg ');
}






copyright notice: This article is original article for blogger. Without permission from blogger,.

must not be reprinted

Latest