【问题标题】:Open a file on the click of button from a textbox path in Asp.net从 Asp.net 中的文本框路径单击按钮打开文件
【发布时间】:2014-07-30 13:00:38
【问题描述】:

使用 Asp.net,我正在读取路径并显示运行时创建的文本框/框中的所有文件路径,如下所示:

这些文件不仅仅是pdf,还有ppt、doc、docx等,我使用的代码是:

 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("&#09;"));
                this.SearchResultPanel.Controls.Add(NewTextP);
                SearchResultPanel.Controls.Add(new LiteralControl("&#09;"));
                this.SearchResultPanel.Controls.Add(ButtonOpen);
            }//end of for

        }//end of try
        catch (DirectoryNotFoundException ex)
        {
            Console.WriteLine("The specified directory does not exist. {0}", ex);
        }

我遇到了一个问题@ButtonOpen.Click += OpenFile(path);,如何在单击“打开”按钮时打开路径关联文件。

有什么建议吗?

【问题讨论】:

  • 我不确定,我认为并非所有浏览器都允许访问本地文件系统,因此您可能会陷入困境。检查这个stackoverflow.com/questions/3152482/running-exe-from-javascript
  • 我在本地机器上使用它,并且将始终使用 chrome,暂时不想添加任何 JS。
  • 抱歉,我再次忘记了它是 Web 应用程序,因此处理程序可能没有在回发时初始化。我会删除我的答案(只是不接受它),然后你最好删除这个问题并重新提出 - 只需使用适当的标签。
  • 我可以使用 ButtonOpen.Click += Process.Start(@path.ToString());

标签: asp.net file-io


【解决方案1】:

请参阅此解决方案。我现在已经用示例进行了测试。如果您从中得到帮助,请标记为已回答。

我硬编码了文件路径。您可以从文本框中获取它并分配给 filePath 变量。

这是链接按钮单击事件。您必须使用 System.IO 和 System .NET 作为额外的库,用于 Path.getExtention() 和 WebClient 类。

    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";
        }
    }
}

}

【讨论】:

  • 嗨 Gihan,非常非常非常...感谢您的解决方案。我会将Button obj 设置为LinkButton。您能否指导我如何在 LinkButton obj 上调用 LinkButton1_Click(),因为在运行时我正在为 directoryEntries[i] 返回的每个路径创建按钮的多个实例。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-19
  • 2011-06-07
相关资源
最近更新 更多