Reputation: 11
I have created a web page and in it I have created a browse button with name "BrowseButton" and a textbox with name "BrowseTextBox"
The backend code is:
protected void BrowseButtonClick(object sender, EventArgs e)
{
FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog();
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.ShowDialog();
BrowseTextBox.Text = openFileDialog1.FileName;
}
but I am getting a ThreadStateException
and I don't know how to handle it....
Upvotes: 1
Views: 7975
Reputation: 13283
This is not going to work since the only way the FolderBrowserDialog
would pop-up is Server-Side
so the program would wait forever for an input.
You should use this Web Control that suits your needs better.
From the MSDN Exmple
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.";
}
I don't think I can explain it better than they did...
Upvotes: 0
Reputation: 34200
You say that you are creating a web page, but your code uses the OpenFileDialog
class from either the Windows Forms or WPF library. These dialogs can't be used on a web application - they're for use when writing Windows applications. The threading error you're seeing is a direct consequence of this.
You can't do anything about this exception: there's no way to use those classes in a web app. Instead, if you want to upload a file, you should perhaps look at the <input type="file"
element of HTML, or perhaps the FileUpload
control in ASP.NET.
Upvotes: 3