Click here to Skip to main content
15,881,852 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
hai

iam developing a website using asp.net,c#(web location:file system ).
can any one help me...


i want to download the file that is displayed in the list box after the search.
i have done the searching of file from the server.

and the code i used is as follows:

C#
resultsList.Items.Clear();
string s = "",s1=""; //to get the file name
// string files = "";
string filename = '*' + fileNameTextBox.Text + '*';
DirectoryInfo MyDir;
ArrayList CellarList = new ArrayList();
MyDir = new DirectoryInfo(Server.MapPath("files/"));
string path = (Server.MapPath("files/"));
//FileInfo[] MyFiles = MyDir.GetFiles("*.*");
string[] fileList = System.IO.Directory.GetFiles(path, filename);

foreach (string file in fileList)
{
  s = file.ToString();
  s1 = s.Replace(path, "");
  resultsList.Items.Add(s1);
}


so plz help....
thanks in advance
Posted
Updated 16-Dec-11 18:08pm
v2
Comments
Orcun Iyigun 17-Dec-11 0:08am    
Added tags.

1 solution

1. You Can Simpally Use window.open(fileurl); command in javascript. it will directly open a file if browser support other wise shows download dialog box.


or you can use a proper download button i.e


C#
string sFilename;
sFilename = Server.MapPath("todown.doc");
byte[] btFile = System.IO.File.ReadAllBytes(sFilename);
Response.Clear();
Response.AddHeader("Content-Disposition", "attachment;filename=" + sFilename + "");
Response.OutputStream.Write(btFile, 0, btFile.Length);
Response.End();
 
Share this answer
 
v2
Comments
Ragi Gopi 17-Dec-11 0:42am    
thanku....

what should i do to display in a panel in web page itself....


thank u for your help...
plz help me for this query tooo...
Technoses 22-Dec-11 7:56am    
you can use iframe and set src=filepath. it runs if browser supports that feature..
Ragi Gopi 22-Dec-11 23:20pm    
can u plz show me an example.....plz help me
Technoses 23-Dec-11 11:25am    
<div>
<iframe src="pdffilename.pdf" style="width:400px; height:400px" />
</div>
Ragi Gopi 3-Jan-12 5:19am    
protected void bl_procedures_Click(object sender, BulletedListEventArgs e)
{

try
{
string path = Server.MapPath("PROCEDURES/" + bl_procedures.Items[e.Index].Text);
System.IO.FileInfo file = new System.IO.FileInfo(path);
Label2.Text = file.Name +"###" + file.FullName;
//Response.Clear();
//Response.AddHeader("Content-Disposition", "inline; filename=" + file.Name);

//Response.AddHeader("content-length", file.Length.ToString());
//Response.ContentType = ReturnExtension(file.Length.ToString());
//Response.TransmitFile(file.FullName);
//Response.WriteFile(file.FullName);
//Response.End();

frmWord.Attributes["src"] = file.Name;
}
catch(Exception ex)
{
Label2.Text = ex.ToString();
}


}


done this code but not displaying data in iframe ..its showing error...what to do???

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