Monday, June 1, 2009

Downloading a File from web server

/This code is used to download a file with open or save dialog box..

Response.Expires = -1;

// scarico un file
string strPath = Server.MapPath("filename");

FileInfo file = new FileInfo(strPath);
// verifica esistenza

if (file.Exists) {
// imposta le headers
Response.Clear();
Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);
Response.AddHeader("Content-Length", file.Length.ToString());
Response.ContentType = "application/octet-stream"; //this changes according to file type

// leggo dal file e scrivo nello stream di risposta
Response.WriteFile(strPath);
Response.End();
}
else
{
Response.Write("Impossibile scaricare il file.");
}

No comments:

Popular Posts