Click here to Skip to main content
12,634,171 members (26,680 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: ASP.NET
Hi all experts,
I have uploaded excel,word,pdf,notepad,image file to server. Now i want to download that file.
but when i download, some file type (excel,word,notepad) don't display their icon. it just display the harddrive.
here are my source code to download:

String fileName = "English Course.xls";
Response.ContentType="application/octet-stream";
Response.AddHeader("Content-Disposition", "attachment;filename=" + fileName);
Response.TransmitFile(Server.MapPath("~/SAP/" + fileName));
Response.End();


Does anybody know how to download all type of file?

Thanks
TONY
Posted 20-Feb-13 23:11pm
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

private void DownloadFile(string filename)
   {
       try
       {
           //Fileinfo class get all Details about user select file.
           FileInfo file = new FileInfo(filename);
           //Checking File is Exists are not using FileInfo object.Exists properties.
           if (file.Exists)
           {
               //Clears all content output from buffer stream.
               Response.ClearContent();
               //ContentType is used to get & set HTTP MIME type of the output stream
               Response.ContentType = "application/octet-stream";
               //AddHeader method is used addHTTP header to the output stream.
               Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);
               Response.AddHeader("Content-Length", file.Length.ToString());
               Response.WriteFile(file.FullName);
               //Send all Current buffered output to the client.
               Response.Flush();
               //Clears all Content output from the buffer stream.
               Response.Clear();
           }

       }
       catch (Exception en)
       {
           Response.Write(en.Message.ToString());

       }
   }
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

Dear Friend,

Analyze the code.

private void loaddetails()
{
try
{
//objinfo.IntInformationID = Convert.ToInt32(Request.QueryString["id"]);
objinfo.IntInformationID = Convert.ToInt32(Session["id"].ToString());
DataSet ds = objinfo.GetInformationPdf();
if (ds.Tables[0].Rows.Count > 0)
{
try
{
ViewState["filename"] = ds.Tables[0].Rows[0]["strDocument"].ToString();
string strPath = string.Empty;
strPath = Server.MapPath("Documents") + "\\" + ViewState["filename"].ToString();
FileInfo file = new FileInfo(strPath);
if (file.Exists)
{

Response.ClearContent();
Response.AddHeader("Content-Disposition", "filename=" + file.Name);
Response.AddHeader("Content-Length", file.Length.ToString());

string[] type = ViewState["filename"].ToString().Split('.');
if (type[1] == "doc")
{
Response.ContentType = "application/msword";
}
else if (type[1] == "docx")
{
Response.ContentType = "application/vnd.msword.document.12";
}
else if (type[1] == "pdf")
{
Response.ContentType = "application/pdf";
}
file.IsReadOnly = true;
Response.TransmitFile(file.FullName);

Response.End();




}
}
catch (Exception ex)
{
Page.RegisterClientScriptBlock("pageClose", "<script>alert('Document Not Available');</script>");
}
}

}
catch (Exception ex)
{
}
}
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

In the Above Solutions if "Response.WriteFile" doesnot works; then try "Response.Redirect" with same parameters.
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web01 | 2.8.161208.2 | Last Updated 21 Feb 2013
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100