pcpaasche
asked on
File open dialog box, ASP.Net/C#
On my web page, I want to let the user be able to browse for a file and select it. The file should be either on his client or on his network. I will in my ASP parse the content of the file. The user should be presented the File open dialog box to pick his file.
I am using ASP.Net with C# as the code behind.
I am using ASP.Net with C# as the code behind.
ASKER
Where should I put this code?
Could you please describe to code I have to put into my ASP-file and the code I have to put into my c-sharp file? How do I present the user for the file open (or file chooser, don't now which name is correct) dialog box?
Could you please describe to code I have to put into my ASP-file and the code I have to put into my c-sharp file? How do I present the user for the file open (or file chooser, don't now which name is correct) dialog box?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks, now I am almost there.
How should the variable fDocument be declared?
How should the variable fDocument be declared?
it should be declared automatically from vs
but it is HtmlInputFile control
B..M
but it is HtmlInputFile control
B..M
i want to get the code for browse the file of the windows folders and get the selected file just like as a open file dialogbox , iwand to implement it in asp.net with c# .it is urjent
ASKER
From the windows forms toolbar in Visual STudio C# design form, drag the OpenFileDialog-widget to the form. Add a browse button to the form and use this code (exerpt):
private System.Windows.Forms.Butto n btnBrowse;
private System.Windows.Forms.OpenF ileDialog openExtensFile;
private void btnBrowse_Click(object sender, System.EventArgs e)
{
OpenFileDialog openExtensFile = new OpenFileDialog();
openExtensFile.Title = "C#" ;
openExtensFile.InitialDire ctory = @"c:\" ;
openExtensFile.Filter = "All files (*.*)|*.*|All files (*.*)|*.*" ;
openExtensFile.FilterIndex = 2 ;
openExtensFile.RestoreDire ctory = true ;
if(openExtensFile.ShowDial og() == DialogResult.OK)
{
// textBox1.Text = fdlg.FileName ;
}
}
========================== ========== ========== ==========
This is my complete code so far:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.DirectoryServices;
namespace nds
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Check Box checkBox1;
private System.Windows.Forms.Check Box checkBox2;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.Label label5;
private System.Windows.Forms.Butto n btnBrowse;
private System.Windows.Forms.OpenF ileDialog openExtensFile;
private System.Windows.Forms.TextB ox txtNdsUsername;
private System.Windows.Forms.TextB ox txtNdsPassword;
private System.Windows.Forms.TextB ox txtNewOu;
private System.Windows.Forms.TextB ox txtLDAPPath;
private System.Windows.Forms.TextB ox txtExtensFilePath;
private System.Windows.Forms.Butto n btnRun;
private System.Windows.Forms.Butto n btnCancel;
private System.DirectoryServices.D irectoryEn try directoryEntryNDS;
private System.Windows.Forms.Butto n txtConnect;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Cont ainer components = null;
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.directoryEntryNDS = new System.DirectoryServices.D irectoryEn try();
this.openExtensFile = new System.Windows.Forms.OpenF ileDialog( );
this.txtNdsUsername = new System.Windows.Forms.TextB ox();
this.txtNdsPassword = new System.Windows.Forms.TextB ox();
this.txtNewOu = new System.Windows.Forms.TextB ox();
this.txtLDAPPath = new System.Windows.Forms.TextB ox();
this.txtExtensFilePath = new System.Windows.Forms.TextB ox();
this.checkBox1 = new System.Windows.Forms.Check Box();
this.checkBox2 = new System.Windows.Forms.Check Box();
this.btnRun = new System.Windows.Forms.Butto n();
this.btnCancel = new System.Windows.Forms.Butto n();
this.label1 = new System.Windows.Forms.Label ();
this.label2 = new System.Windows.Forms.Label ();
this.label3 = new System.Windows.Forms.Label ();
this.label4 = new System.Windows.Forms.Label ();
this.label5 = new System.Windows.Forms.Label ();
this.btnBrowse = new System.Windows.Forms.Butto n();
this.txtConnect = new System.Windows.Forms.Butto n();
this.SuspendLayout();
//
// directoryEntryNDS
//
this.directoryEntryNDS.Pat h = "LDAP://DC=troperpaasc,DC= ementor,DC =no\");";
//
// openExtensFile
//
this.openExtensFile.FileOk += new System.ComponentModel.Canc elEventHan dler(this. openExtens File_FileO k);
//
// txtNdsUsername
//
this.txtNdsUsername.Locati on = new System.Drawing.Point(200, 40);
this.txtNdsUsername.Name = "txtNdsUsername";
this.txtNdsUsername.Size = new System.Drawing.Size(232, 20);
this.txtNdsUsername.TabInd ex = 0;
this.txtNdsUsername.Text = "";
//
// txtNdsPassword
//
this.txtNdsPassword.Locati on = new System.Drawing.Point(200, 80);
this.txtNdsPassword.Name = "txtNdsPassword";
this.txtNdsPassword.Size = new System.Drawing.Size(232, 20);
this.txtNdsPassword.TabInd ex = 1;
this.txtNdsPassword.Text = "";
//
// txtNewOu
//
this.txtNewOu.Location = new System.Drawing.Point(200, 160);
this.txtNewOu.Name = "txtNewOu";
this.txtNewOu.Size = new System.Drawing.Size(232, 20);
this.txtNewOu.TabIndex = 2;
this.txtNewOu.Text = "";
//
// txtLDAPPath
//
this.txtLDAPPath.Location = new System.Drawing.Point(200, 120);
this.txtLDAPPath.Name = "txtLDAPPath";
this.txtLDAPPath.Size = new System.Drawing.Size(232, 20);
this.txtLDAPPath.TabIndex = 3;
this.txtLDAPPath.Text = "";
//
// txtExtensFilePath
//
this.txtExtensFilePath.Loc ation = new System.Drawing.Point(200, 200);
this.txtExtensFilePath.Nam e = "txtExtensFilePath";
this.txtExtensFilePath.Siz e = new System.Drawing.Size(232, 20);
this.txtExtensFilePath.Tab Index = 4;
this.txtExtensFilePath.Tex t = "";
//
// checkBox1
//
this.checkBox1.Location = new System.Drawing.Point(200, 248);
this.checkBox1.Name = "checkBox1";
this.checkBox1.TabIndex = 5;
this.checkBox1.Text = "checkBox1";
//
// checkBox2
//
this.checkBox2.Location = new System.Drawing.Point(200, 280);
this.checkBox2.Name = "checkBox2";
this.checkBox2.TabIndex = 6;
this.checkBox2.Text = "checkBox2";
//
// btnRun
//
this.btnRun.Location = new System.Drawing.Point(160, 328);
this.btnRun.Name = "btnRun";
this.btnRun.Size = new System.Drawing.Size(120, 40);
this.btnRun.TabIndex = 7;
this.btnRun.Text = "Importer/oppdater brukere";
//
// btnCancel
//
this.btnCancel.Location = new System.Drawing.Point(312, 328);
this.btnCancel.Name = "btnCancel";
this.btnCancel.Size = new System.Drawing.Size(120, 40);
this.btnCancel.TabIndex = 8;
this.btnCancel.Text = "Avbryt";
//
// label1
//
this.label1.Location = new System.Drawing.Point(88, 48);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(96, 16);
this.label1.TabIndex = 9;
this.label1.Text = "NDS Brukernavn";
//
// label2
//
this.label2.Location = new System.Drawing.Point(88, 88);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(96, 16);
this.label2.TabIndex = 10;
this.label2.Text = "NDS Passord";
//
// label3
//
this.label3.Location = new System.Drawing.Point(88, 120);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(96, 16);
this.label3.TabIndex = 11;
this.label3.Text = "LDAP path:";
//
// label4
//
this.label4.Location = new System.Drawing.Point(88, 160);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(96, 16);
this.label4.TabIndex = 12;
this.label4.Text = "Ny OU";
//
// label5
//
this.label5.Location = new System.Drawing.Point(88, 200);
this.label5.Name = "label5";
this.label5.Size = new System.Drawing.Size(96, 16);
this.label5.TabIndex = 13;
this.label5.Text = "Fil fra Extens";
//
// btnBrowse
//
this.btnBrowse.Location = new System.Drawing.Point(456, 200);
this.btnBrowse.Name = "btnBrowse";
this.btnBrowse.Size = new System.Drawing.Size(104, 24);
this.btnBrowse.TabIndex = 14;
this.btnBrowse.Text = "Browse...";
this.btnBrowse.Click += new System.EventHandler(this.b tnBrowse_C lick);
//
// txtConnect
//
this.txtConnect.Location = new System.Drawing.Point(464, 56);
this.txtConnect.Name = "txtConnect";
this.txtConnect.Size = new System.Drawing.Size(144, 32);
this.txtConnect.TabIndex = 15;
this.txtConnect.Text = "Connect LDAP";
this.txtConnect.Click += new System.EventHandler(this.t xtConnect_ Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(664, 462);
this.Controls.Add(this.txt Connect);
this.Controls.Add(this.btn Browse);
this.Controls.Add(this.lab el5);
this.Controls.Add(this.lab el4);
this.Controls.Add(this.lab el3);
this.Controls.Add(this.lab el2);
this.Controls.Add(this.lab el1);
this.Controls.Add(this.btn Cancel);
this.Controls.Add(this.btn Run);
this.Controls.Add(this.che ckBox2);
this.Controls.Add(this.che ckBox1);
this.Controls.Add(this.txt ExtensFile Path);
this.Controls.Add(this.txt LDAPPath);
this.Controls.Add(this.txt NewOu);
this.Controls.Add(this.txt NdsPasswor d);
this.Controls.Add(this.txt NdsUsernam e);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void btnBrowse_Click(object sender, System.EventArgs e)
{
OpenFileDialog openExtensFile = new OpenFileDialog();
openExtensFile.Title = "C#" ;
openExtensFile.InitialDire ctory = @"c:\" ;
openExtensFile.Filter = "All files (*.*)|*.*|All files (*.*)|*.*" ;
openExtensFile.FilterIndex = 2 ;
openExtensFile.RestoreDire ctory = true ;
if(openExtensFile.ShowDial og() == DialogResult.OK)
{
// textBox1.Text = fdlg.FileName ;
}
}
private DirectoryEntry getLDAPHandle(string userName, string password,
string LdapPath)
{
return new DirectoryEntry();
}
private void createOu(string LdapPath, string OuName) {}
private bool validateExtensFile()
{
return true;
}
private string makePassword(int length) {
return "password";
}
private bool doesExistUser(string userName) {
return true;
}
private void getUserId(string firstName, string lastName) {}
private void setPassword (DirectoryEntry de) {}
private void setAttributeValue (string attribute, string value) {}
private void writeLog(string functionName, string logText) {}
private void writeReceipt(){}
private void txtConnect_Click(object sender, System.EventArgs e)
{
string ldap = "LDAP://OU=skoler,DC=vgs,D C=stfk,DC= int";
directoryEntryNDS = new DirectoryEntry(ldap, txtNdsUsername.Text, txtNdsPassword.Text);
}
}
}
Hope this helps.
private System.Windows.Forms.Butto
private System.Windows.Forms.OpenF
private void btnBrowse_Click(object sender, System.EventArgs e)
{
OpenFileDialog openExtensFile = new OpenFileDialog();
openExtensFile.Title = "C#" ;
openExtensFile.InitialDire
openExtensFile.Filter = "All files (*.*)|*.*|All files (*.*)|*.*" ;
openExtensFile.FilterIndex
openExtensFile.RestoreDire
if(openExtensFile.ShowDial
{
// textBox1.Text = fdlg.FileName ;
}
}
==========================
This is my complete code so far:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.DirectoryServices;
namespace nds
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Check
private System.Windows.Forms.Check
private System.Windows.Forms.Label
private System.Windows.Forms.Label
private System.Windows.Forms.Label
private System.Windows.Forms.Label
private System.Windows.Forms.Label
private System.Windows.Forms.Butto
private System.Windows.Forms.OpenF
private System.Windows.Forms.TextB
private System.Windows.Forms.TextB
private System.Windows.Forms.TextB
private System.Windows.Forms.TextB
private System.Windows.Forms.TextB
private System.Windows.Forms.Butto
private System.Windows.Forms.Butto
private System.DirectoryServices.D
private System.Windows.Forms.Butto
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Cont
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.directoryEntryNDS = new System.DirectoryServices.D
this.openExtensFile = new System.Windows.Forms.OpenF
this.txtNdsUsername = new System.Windows.Forms.TextB
this.txtNdsPassword = new System.Windows.Forms.TextB
this.txtNewOu = new System.Windows.Forms.TextB
this.txtLDAPPath = new System.Windows.Forms.TextB
this.txtExtensFilePath = new System.Windows.Forms.TextB
this.checkBox1 = new System.Windows.Forms.Check
this.checkBox2 = new System.Windows.Forms.Check
this.btnRun = new System.Windows.Forms.Butto
this.btnCancel = new System.Windows.Forms.Butto
this.label1 = new System.Windows.Forms.Label
this.label2 = new System.Windows.Forms.Label
this.label3 = new System.Windows.Forms.Label
this.label4 = new System.Windows.Forms.Label
this.label5 = new System.Windows.Forms.Label
this.btnBrowse = new System.Windows.Forms.Butto
this.txtConnect = new System.Windows.Forms.Butto
this.SuspendLayout();
//
// directoryEntryNDS
//
this.directoryEntryNDS.Pat
//
// openExtensFile
//
this.openExtensFile.FileOk
//
// txtNdsUsername
//
this.txtNdsUsername.Locati
this.txtNdsUsername.Name = "txtNdsUsername";
this.txtNdsUsername.Size = new System.Drawing.Size(232, 20);
this.txtNdsUsername.TabInd
this.txtNdsUsername.Text = "";
//
// txtNdsPassword
//
this.txtNdsPassword.Locati
this.txtNdsPassword.Name = "txtNdsPassword";
this.txtNdsPassword.Size = new System.Drawing.Size(232, 20);
this.txtNdsPassword.TabInd
this.txtNdsPassword.Text = "";
//
// txtNewOu
//
this.txtNewOu.Location = new System.Drawing.Point(200, 160);
this.txtNewOu.Name = "txtNewOu";
this.txtNewOu.Size = new System.Drawing.Size(232, 20);
this.txtNewOu.TabIndex = 2;
this.txtNewOu.Text = "";
//
// txtLDAPPath
//
this.txtLDAPPath.Location = new System.Drawing.Point(200, 120);
this.txtLDAPPath.Name = "txtLDAPPath";
this.txtLDAPPath.Size = new System.Drawing.Size(232, 20);
this.txtLDAPPath.TabIndex = 3;
this.txtLDAPPath.Text = "";
//
// txtExtensFilePath
//
this.txtExtensFilePath.Loc
this.txtExtensFilePath.Nam
this.txtExtensFilePath.Siz
this.txtExtensFilePath.Tab
this.txtExtensFilePath.Tex
//
// checkBox1
//
this.checkBox1.Location = new System.Drawing.Point(200, 248);
this.checkBox1.Name = "checkBox1";
this.checkBox1.TabIndex = 5;
this.checkBox1.Text = "checkBox1";
//
// checkBox2
//
this.checkBox2.Location = new System.Drawing.Point(200, 280);
this.checkBox2.Name = "checkBox2";
this.checkBox2.TabIndex = 6;
this.checkBox2.Text = "checkBox2";
//
// btnRun
//
this.btnRun.Location = new System.Drawing.Point(160, 328);
this.btnRun.Name = "btnRun";
this.btnRun.Size = new System.Drawing.Size(120, 40);
this.btnRun.TabIndex = 7;
this.btnRun.Text = "Importer/oppdater brukere";
//
// btnCancel
//
this.btnCancel.Location = new System.Drawing.Point(312, 328);
this.btnCancel.Name = "btnCancel";
this.btnCancel.Size = new System.Drawing.Size(120, 40);
this.btnCancel.TabIndex = 8;
this.btnCancel.Text = "Avbryt";
//
// label1
//
this.label1.Location = new System.Drawing.Point(88, 48);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(96, 16);
this.label1.TabIndex = 9;
this.label1.Text = "NDS Brukernavn";
//
// label2
//
this.label2.Location = new System.Drawing.Point(88, 88);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(96, 16);
this.label2.TabIndex = 10;
this.label2.Text = "NDS Passord";
//
// label3
//
this.label3.Location = new System.Drawing.Point(88, 120);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(96, 16);
this.label3.TabIndex = 11;
this.label3.Text = "LDAP path:";
//
// label4
//
this.label4.Location = new System.Drawing.Point(88, 160);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(96, 16);
this.label4.TabIndex = 12;
this.label4.Text = "Ny OU";
//
// label5
//
this.label5.Location = new System.Drawing.Point(88, 200);
this.label5.Name = "label5";
this.label5.Size = new System.Drawing.Size(96, 16);
this.label5.TabIndex = 13;
this.label5.Text = "Fil fra Extens";
//
// btnBrowse
//
this.btnBrowse.Location = new System.Drawing.Point(456, 200);
this.btnBrowse.Name = "btnBrowse";
this.btnBrowse.Size = new System.Drawing.Size(104, 24);
this.btnBrowse.TabIndex = 14;
this.btnBrowse.Text = "Browse...";
this.btnBrowse.Click += new System.EventHandler(this.b
//
// txtConnect
//
this.txtConnect.Location = new System.Drawing.Point(464, 56);
this.txtConnect.Name = "txtConnect";
this.txtConnect.Size = new System.Drawing.Size(144, 32);
this.txtConnect.TabIndex = 15;
this.txtConnect.Text = "Connect LDAP";
this.txtConnect.Click += new System.EventHandler(this.t
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(664, 462);
this.Controls.Add(this.txt
this.Controls.Add(this.btn
this.Controls.Add(this.lab
this.Controls.Add(this.lab
this.Controls.Add(this.lab
this.Controls.Add(this.lab
this.Controls.Add(this.lab
this.Controls.Add(this.btn
this.Controls.Add(this.btn
this.Controls.Add(this.che
this.Controls.Add(this.che
this.Controls.Add(this.txt
this.Controls.Add(this.txt
this.Controls.Add(this.txt
this.Controls.Add(this.txt
this.Controls.Add(this.txt
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void btnBrowse_Click(object sender, System.EventArgs e)
{
OpenFileDialog openExtensFile = new OpenFileDialog();
openExtensFile.Title = "C#" ;
openExtensFile.InitialDire
openExtensFile.Filter = "All files (*.*)|*.*|All files (*.*)|*.*" ;
openExtensFile.FilterIndex
openExtensFile.RestoreDire
if(openExtensFile.ShowDial
{
// textBox1.Text = fdlg.FileName ;
}
}
private DirectoryEntry getLDAPHandle(string userName, string password,
string LdapPath)
{
return new DirectoryEntry();
}
private void createOu(string LdapPath, string OuName) {}
private bool validateExtensFile()
{
return true;
}
private string makePassword(int length) {
return "password";
}
private bool doesExistUser(string userName) {
return true;
}
private void getUserId(string firstName, string lastName) {}
private void setPassword (DirectoryEntry de) {}
private void setAttributeValue (string attribute, string value) {}
private void writeLog(string functionName, string logText) {}
private void writeReceipt(){}
private void txtConnect_Click(object sender, System.EventArgs e)
{
string ldap = "LDAP://OU=skoler,DC=vgs,D
directoryEntryNDS = new DirectoryEntry(ldap, txtNdsUsername.Text, txtNdsPassword.Text);
}
}
}
Hope this helps.
But here i have one question.
when i click on browse button i want to open file with particular extension like .txt or only all files or .htm in web aplication not in window
It shows only particular extension file how i will do in web application.
when i click on browse button i want to open file with particular extension like .txt or only all files or .htm in web aplication not in window
It shows only particular extension file how i will do in web application.
<input type="file" runat="server" id="fDocument">
when you click on the Browse button and select a file then click button and submit the page
then you can use fDcoument.PostedFile.SaveA
or
fDocument.PostedFile.Input
HTH
B..M