Click here to Skip to main content
15,884,099 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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:

C#
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

C#
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());

       }
   }
 
Share this answer
 
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)
{
}
}
 
Share this answer
 
In the Above Solutions if "Response.WriteFile" doesnot works; then try "Response.Redirect" with same parameters.
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900