protected void Button1_Click(object sender, EventArgs e)
{
// Get the physical Path of the file(test.doc)string filepath = Server.MapPath("images/5.jpg")
FileInfo file = new FileInfo(filepath);
// Checking if file exists
if (file.Exists)
{
// Clear the content of the responseResponse.ClearContent();
Response.Clear();
// LINE1: Add the file name and attachment, which will force the open/cance/save dialog to show, to the headerResponse.AddHeader(
"Content-Disposition", "attachment; filename=" + file.Name); // Add the file size into the response headerResponse.AddHeader(
"Content-Length", file.Length.ToString()); // Set the ContentTypeResponse.ContentType = ReturnExtension(file.
Response.TransmitFile(file.
Response.Flush();
// End the responseResponse.End();
}
}
private string ReturnExtension(string fileExtension){
switch (fileExtension){
case ".htm":case ".html":
case ".log":
return "text/HTML";
case ".txt":
return "text/plain";
case ".doc":
return "application/ms-word";
case ".tiff":
case ".tif":
return "image/tiff";
case ".asf":
return "video/x-ms-asf";
case ".avi":
return "video/avi";
case ".zip":
return "application/zip";
case ".xls":
case ".csv":
return "application/vnd.ms-excel";
case ".gif":
return "image/gif";
case ".jpg":
case "jpeg":
return "image/jpeg";
case ".bmp":
return "image/bmp";
case ".wav":
return "audio/wav";
case ".mp3":
return "audio/mpeg3";
case ".mpg":
case "mpeg":
return "video/mpeg";
case ".rtf":
return "application/rtf";
case ".asp":
return "text/asp";
case ".pdf":
return "application/pdf";
case ".fdf":
return "application/vnd.fdf";
case ".ppt":
return "application/mspowerpoint";
case ".dwg":
return "image/vnd.dwg";
case ".msg":
return "application/msoutlook";
case ".xml":
case ".sdxl":
return "application/xml";
case ".xdp":
return "application/vnd.adobe.xdp+
default:
return "application/octet-stream";
}
}


No comments:
Post a Comment