Solved

File open dialog box, ASP.Net/C#

Posted on 2004-04-22
9
139,844 Views
Last Modified: 2012-08-13
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.

0
Comment
Question by:pcpaasche
9 Comments
 
LVL 28

Expert Comment

by:mmarinov
ID: 10893195
you have to add
<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.SaveAs method to store it on your hard disk
or
fDocument.PostedFile.InputStream property to ( from MSDN ): Gets a Stream object which points to an uploaded file to prepare for reading the contents of the file.

HTH
B..M
0
 

Author Comment

by:pcpaasche
ID: 10897001
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?
0
 
LVL 28

Accepted Solution

by:
mmarinov earned 125 total points
ID: 10897444
here is an example:

aspx page
      <body>
            <form id="Form1" method="post" runat="server">
                  <input type="file" runat="server" id="fDocument">
                  <asp:Button id="Button1" runat="server" Text="Button"></asp:Button>
                  <asp:CheckBox id="CheckBox1" runat="server" Text="Save"></asp:CheckBox>
            </form>
      </body>

create Button1_Click event
            private void Button1_Click(object sender, System.EventArgs e)
            {
                  System.IO.Stream s ;
                  if ( CheckBox1.Checked )
                        fDocument.PostedFile.SaveAs("c:\\document.extension");
                  else
                        s = fDocument.PostedFile.InputStream;

            }

the functionallity is depending on would you want to save the file or not

B..M
0
ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

 

Author Comment

by:pcpaasche
ID: 10897901
Thanks, now I am almost there.

How should the variable fDocument be declared?

0
 
LVL 28

Expert Comment

by:mmarinov
ID: 10898015
it should be declared automatically from vs
but it is HtmlInputFile control

B..M
0
 

Expert Comment

by:VineethJvenattu
ID: 12838450
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
0
 

Author Comment

by:pcpaasche
ID: 12838617
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.Button btnBrowse;
        private System.Windows.Forms.OpenFileDialog openExtensFile;


private void btnBrowse_Click(object sender, System.EventArgs e)
        {
            OpenFileDialog openExtensFile = new OpenFileDialog();
            openExtensFile.Title = "C#" ;
            openExtensFile.InitialDirectory = @"c:\" ;
            openExtensFile.Filter = "All files (*.*)|*.*|All files (*.*)|*.*" ;
            openExtensFile.FilterIndex = 2 ;
            openExtensFile.RestoreDirectory = true ;
            if(openExtensFile.ShowDialog() == 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.CheckBox checkBox1;
        private System.Windows.Forms.CheckBox 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.Button btnBrowse;
        private System.Windows.Forms.OpenFileDialog openExtensFile;
        private System.Windows.Forms.TextBox txtNdsUsername;
        private System.Windows.Forms.TextBox txtNdsPassword;
        private System.Windows.Forms.TextBox txtNewOu;
        private System.Windows.Forms.TextBox txtLDAPPath;
        private System.Windows.Forms.TextBox txtExtensFilePath;
        private System.Windows.Forms.Button btnRun;
        private System.Windows.Forms.Button btnCancel;
        private System.DirectoryServices.DirectoryEntry directoryEntryNDS;
        private System.Windows.Forms.Button txtConnect;
            /// <summary>
            /// Required designer variable.
            /// </summary>
            private System.ComponentModel.Container 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.DirectoryEntry();
            this.openExtensFile = new System.Windows.Forms.OpenFileDialog();
            this.txtNdsUsername = new System.Windows.Forms.TextBox();
            this.txtNdsPassword = new System.Windows.Forms.TextBox();
            this.txtNewOu = new System.Windows.Forms.TextBox();
            this.txtLDAPPath = new System.Windows.Forms.TextBox();
            this.txtExtensFilePath = new System.Windows.Forms.TextBox();
            this.checkBox1 = new System.Windows.Forms.CheckBox();
            this.checkBox2 = new System.Windows.Forms.CheckBox();
            this.btnRun = new System.Windows.Forms.Button();
            this.btnCancel = new System.Windows.Forms.Button();
            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.Button();
            this.txtConnect = new System.Windows.Forms.Button();
            this.SuspendLayout();
            //
            // directoryEntryNDS
            //
            this.directoryEntryNDS.Path = "LDAP://DC=troperpaasc,DC=ementor,DC=no\");";
            //
            // openExtensFile
            //
            this.openExtensFile.FileOk += new System.ComponentModel.CancelEventHandler(this.openExtensFile_FileOk);
            //
            // txtNdsUsername
            //
            this.txtNdsUsername.Location = new System.Drawing.Point(200, 40);
            this.txtNdsUsername.Name = "txtNdsUsername";
            this.txtNdsUsername.Size = new System.Drawing.Size(232, 20);
            this.txtNdsUsername.TabIndex = 0;
            this.txtNdsUsername.Text = "";
            //
            // txtNdsPassword
            //
            this.txtNdsPassword.Location = new System.Drawing.Point(200, 80);
            this.txtNdsPassword.Name = "txtNdsPassword";
            this.txtNdsPassword.Size = new System.Drawing.Size(232, 20);
            this.txtNdsPassword.TabIndex = 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.Location = new System.Drawing.Point(200, 200);
            this.txtExtensFilePath.Name = "txtExtensFilePath";
            this.txtExtensFilePath.Size = new System.Drawing.Size(232, 20);
            this.txtExtensFilePath.TabIndex = 4;
            this.txtExtensFilePath.Text = "";
            //
            // 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.btnBrowse_Click);
            //
            // 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.txtConnect_Click);
            //
            // Form1
            //
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(664, 462);
            this.Controls.Add(this.txtConnect);
            this.Controls.Add(this.btnBrowse);
            this.Controls.Add(this.label5);
            this.Controls.Add(this.label4);
            this.Controls.Add(this.label3);
            this.Controls.Add(this.label2);
            this.Controls.Add(this.label1);
            this.Controls.Add(this.btnCancel);
            this.Controls.Add(this.btnRun);
            this.Controls.Add(this.checkBox2);
            this.Controls.Add(this.checkBox1);
            this.Controls.Add(this.txtExtensFilePath);
            this.Controls.Add(this.txtLDAPPath);
            this.Controls.Add(this.txtNewOu);
            this.Controls.Add(this.txtNdsPassword);
            this.Controls.Add(this.txtNdsUsername);
            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.InitialDirectory = @"c:\" ;
            openExtensFile.Filter = "All files (*.*)|*.*|All files (*.*)|*.*" ;
            openExtensFile.FilterIndex = 2 ;
            openExtensFile.RestoreDirectory = true ;
            if(openExtensFile.ShowDialog() == 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,DC=stfk,DC=int";
            directoryEntryNDS = new DirectoryEntry(ldap, txtNdsUsername.Text, txtNdsPassword.Text);


       
        }

     

       

   





       
      }
}

Hope this helps.




0
 

Expert Comment

by:prashant_24patil
ID: 22666089
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.


0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question