1

我创建了一个网页,并在其中创建了一个名为“BrowseButton”的浏览按钮和一个名为“BrowseTextBox”的文本框

后端代码是:

protected void BrowseButtonClick(object sender, EventArgs e)

        {
            FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog();
            OpenFileDialog openFileDialog1 = new OpenFileDialog();
            openFileDialog1.ShowDialog();
            BrowseTextBox.Text = openFileDialog1.FileName;
        }

但我得到了一个ThreadStateException,我不知道如何处理它....

4

2 回答 2

3

您说您正在创建一个网页,但您的代码使用OpenFileDialog来自 Windows 窗体或 WPF 库的类。这些对话框不能在 Web 应用程序上使用——它们是在编写 Windows 应用程序时使用的。您看到的线程错误是由此产生的直接后果。

你不能对这个异常做任何事情:没有办法在 web 应用程序中使用这些类。相反,如果你想上传一个文件,你也许应该查看<input type="file"HTML 的元素,或者也许是 ASP.NET 中的FileUpload控件

于 2013-04-03T15:47:43.240 回答
0

这是行不通的,因为FolderBrowserDialog弹出的唯一方法是Server-Side程序将永远等待输入。

您应该使用更适合您需要的Web控件。

来自 MSDN 示例

 protected void UploadButton_Click(object sender, EventArgs e)
{
    // Save the uploaded file to an "Uploads" directory
    // that already exists in the file system of the 
    // currently executing ASP.NET application.  
    // Creating an "Uploads" directory isolates uploaded 
    // files in a separate directory. This helps prevent
    // users from overwriting existing application files by
    // uploading files with names like "Web.config".
    string saveDir = @"\Uploads\";

    // Get the physical file system path for the currently
    // executing application.
    string appPath = Request.PhysicalApplicationPath;

    // Before attempting to save the file, verify
    // that the FileUpload control contains a file.
    if (FileUpload1.HasFile)
    {
        string savePath = appPath + saveDir +
            Server.HtmlEncode(FileUpload1.FileName);

        // Call the SaveAs method to save the 
        // uploaded file to the specified path.
        // This example does not perform all
        // the necessary error checking.               
        // If a file with the same name
        // already exists in the specified path,  
        // the uploaded file overwrites it.
        FileUpload1.SaveAs(savePath);

        // Notify the user that the file was uploaded successfully.
        UploadStatusLabel.Text = "Your file was uploaded successfully.";

    }
    else
    {
        // Notify the user that a file was not uploaded.
        UploadStatusLabel.Text = "You did not specify a file to upload.";   
    }

我不认为我能比他们解释得更好...

于 2013-04-03T15:47:52.707 回答