To download any file from server use the following code:
string file_path="~/files/file.xps"; //relative path of your file on server
string ext = file_path.Substring(file_path.LastIndexOf('.')+1).ToLower (); //get the extension of file
string filename="myfile."+ext; //your file will be downloaded with this name
System.Web.HttpResponse response = System.Web.HttpContext.Current.Response;
response.ClearContent();
response.Clear();
if (ext == "pdf")
response.ContentType = "application/pdf";
else
if (ext == "txt")
response.ContentType = "text/plain";
else
if (ext == "docx")
response.ContentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
else
if (ext == "doc")
response.ContentType = "application/msword";
else
if (ext == "xlsx")
response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
else
if (ext == "xls")
response.ContentType = "application/vnd.ms-excel";
else
if (ext == "xps")
response.ContentType = "application/vnd.ms-xpsdocument";
response.AddHeader("Content-Disposition", "attachment; filename=" +filename+ ";"); response.TransmitFile(Server.MapPath(file_path ));
string file_path="~/files/file.xps"; //relative path of your file on server
string ext = file_path.Substring(file_path.LastIndexOf('.')+1).ToLower (); //get the extension of file
string filename="myfile."+ext; //your file will be downloaded with this name
System.Web.HttpResponse response = System.Web.HttpContext.Current.Response;
response.ClearContent();
response.Clear();
if (ext == "pdf")
response.ContentType = "application/pdf";
else
if (ext == "txt")
response.ContentType = "text/plain";
else
if (ext == "docx")
response.ContentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
else
if (ext == "doc")
response.ContentType = "application/msword";
else
if (ext == "xlsx")
response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
else
if (ext == "xls")
response.ContentType = "application/vnd.ms-excel";
else
if (ext == "xps")
response.ContentType = "application/vnd.ms-xpsdocument";
response.AddHeader("Content-Disposition", "attachment; filename=" +filename+ ";"); response.TransmitFile(Server.MapPath(file_path ));
response.Flush();
response.End();
No comments:
Post a Comment