Reputation: 67
Using Asp.net, I'm reading a path and showing all the files path in a runtime created Text box/boxes as follows:
These Files not just pdf but ppt, doc,docx, etc and the code which i used is:
protected void SearchButton_Click(object sender, EventArgs e)
{
string dir = @"D:\file path\";
SearchResultPanel.Visible = true;
try
{
//Set the current directory.
Directory.SetCurrentDirectory(dir);
//search all files in path
string[] directoryEntries = Directory.GetFiles(dir, "*.*", SearchOption.AllDirectories);
int fill = directoryEntries.Length - 1;
//Total count of files & Decoration
SearchResultPanel.Controls.Add(new LiteralControl("<br />"));
Label leb = new Label();
SearchResultPanel.Controls.Add(new LiteralControl("The total files count: "));
leb.Text = directoryEntries.Length.ToString();
this.SearchResultPanel.Controls.Add(leb);
SearchResultPanel.Controls.Add(new LiteralControl("<hr />"));
//retriving the files in new labels
for (int i = 0; i < fill; i++)
{
TextBox NewText = new TextBox();
NewText.BorderStyle = BorderStyle.NotSet;
NewText.TextMode = TextBoxMode.MultiLine;
NewText.Width = 380;
NewText.Height = 40;
TextBox NewTextP = new TextBox();
NewTextP.BorderStyle = BorderStyle.NotSet;
NewTextP.TextMode = TextBoxMode.MultiLine;
NewTextP.Width = 380;
NewTextP.Height = 40;
Button ButtonOpen = new Button();
ButtonOpen.Text = "Open";
//fill the text boxes with file entries
String path = directoryEntries[i];
NewText.Text = directoryEntries[i];
NewTextP.Text = "Dummy Paragraph";
**ButtonOpen.Click += OpenFile(path);**
//new line
SearchResultPanel.Controls.Add(new LiteralControl("<br />"));
//show the text box
this.SearchResultPanel.Controls.Add(NewText);
SearchResultPanel.Controls.Add(new LiteralControl("	"));
this.SearchResultPanel.Controls.Add(NewTextP);
SearchResultPanel.Controls.Add(new LiteralControl("	"));
this.SearchResultPanel.Controls.Add(ButtonOpen);
}//end of for
}//end of try
catch (DirectoryNotFoundException ex)
{
Console.WriteLine("The specified directory does not exist. {0}", ex);
}
I'm getting a problem @ ButtonOpen.Click += OpenFile(path);
as how to open the path associate file on the click of OPEN button.
Any Suggessions?
Upvotes: 0
Views: 4316
Reputation: 43
Please See this solution. I have tested now with sample.Please Mark as answered if you got help from this.
I hardcoded the file path. You can get it from text box and assign to filePath variable.
This is Link Button Click event. You have to use System.IO and System .NET as extra libraries, which is for Path.getExtention() and WebClient Class.
protected void LinkButton1_Click(object sender, EventArgs e)
{
string filePath = "C:\\GIHAN\\Employee Profile - Senior Engineer Technology.docx";
string fileExtention = Path.GetExtension(filePath);
WebClient client = new WebClient();
Byte[] buffer = client.DownloadData(filePath);
Response.ContentType = ReturnExtension(fileExtention);
Response.AddHeader("content-length", buffer.Length.ToString());
Response.BinaryWrite(buffer);
}
protected void Button1_Click(object sender, EventArgs e)
{
}
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+xml";
default:
return "application/octet-stream";
}
}
}
}
Upvotes: 1