Forzare il download di una risorsa sul server
private void Page_Load(object sender, System.EventArgs e)
{
Response.ContentType = "Application/pdf";
string FilePath = MapPath("acrobat.pdf");
Response.WriteFile(FilePath);
Response.End();
}
private void DownloadFile( string fname, bool forceDownload )
{
string path = MapPath( fname );
string name = Path.GetFileName( path );
string ext = Path.GetExtension( path );
string type = "";
if ( ext != null )
{
switch( ext.ToLower() )
{
case ".htm":
case ".html":
type = "text/HTML";
break;
case ".txt":
type = "text/plain";
break;
case ".doc":
case ".rtf":
type = "Application/msword";
break;
}
}
if ( forceDownload )
{
Response.AppendHeader( "content-disposition",
"attachment; filename=" + name );
}
if ( type != "" )
Response.ContentType = type;
Response.WriteFile( path );
Response.End();
}
Scarica il Codice...
Stampa la pagina