Browse for a filename?

Discussion in 'ASP .Net' started by UJ, Jun 8, 2005.

  1. UJ

    UJ Guest

    How can I add a browse button to my asp.net page? What I need is a button
    they can press that will then let them select the file to upload to the
    server.

    And if anybody has any good code on how to upload a file to the server I'd
    appreciate that also.

    TIA - Jeffrey.
     
    UJ, Jun 8, 2005
    #1
    1. Advertising

  2. UJ

    Bob Weiner Guest

    Here is something simple and straightforward:


    On the client side:
    <INPUT id="File1" type="file" runat="server" name="FileUpload">
    <asp:button id="btnUpload" runat="server"></asp:button>


    On the Server:

    // these will be added automatically if you are using Visual Studio
    protected System.Web.UI.WebControls.Button btnUpload;
    protected System.Web.UI.HtmlControls.HtmlInputFile File1;
    protected System.Web.UI.WebControls.Label lblReply;

    this.btnUpload.Click += new System.EventHandler(this.btnUpload_Click);
    // end of automatically added lines

    // this function assumes InQueue is defined
    private void btnUpload_Click(object sender, System.EventArgs e) {
    if (File1.PostedFile.FileName == "") {
    lblReply.Text = "You must specify the file first.";
    } else {
    string procID = DateTime.Now.ToString("MMddHHmm");
    string filename = InQueue + "NewUsers." + procID + ".xls";
    try {
    if (File.Exists(filename)) {
    lblReply.Text = "File already exists";
    } else {
    File1.PostedFile.SaveAs (filename);
    lblReply.Text = "File " + filename + " has been received.";
    }
    } catch (Exception err) {
    lblReply.Text = err.Message;
    }
    }
    }

    "UJ" <> wrote in message
    news:...
    > How can I add a browse button to my asp.net page? What I need is a button
    > they can press that will then let them select the file to upload to the
    > server.
    >
    > And if anybody has any good code on how to upload a file to the server I'd
    > appreciate that also.
    >
    > TIA - Jeffrey.
    >
    >
     
    Bob Weiner, Jun 8, 2005
    #2
    1. Advertising

  3. UJ

    UJ Guest

    That's perfect. Thanks!

    "Bob Weiner" <> wrote in message
    news:O$...
    > Here is something simple and straightforward:
    >
    >
    > On the client side:
    > <INPUT id="File1" type="file" runat="server" name="FileUpload">
    > <asp:button id="btnUpload" runat="server"></asp:button>
    >
    >
    > On the Server:
    >
    > // these will be added automatically if you are using Visual Studio
    > protected System.Web.UI.WebControls.Button btnUpload;
    > protected System.Web.UI.HtmlControls.HtmlInputFile File1;
    > protected System.Web.UI.WebControls.Label lblReply;
    >
    > this.btnUpload.Click += new System.EventHandler(this.btnUpload_Click);
    > // end of automatically added lines
    >
    > // this function assumes InQueue is defined
    > private void btnUpload_Click(object sender, System.EventArgs e) {
    > if (File1.PostedFile.FileName == "") {
    > lblReply.Text = "You must specify the file first.";
    > } else {
    > string procID = DateTime.Now.ToString("MMddHHmm");
    > string filename = InQueue + "NewUsers." + procID + ".xls";
    > try {
    > if (File.Exists(filename)) {
    > lblReply.Text = "File already exists";
    > } else {
    > File1.PostedFile.SaveAs (filename);
    > lblReply.Text = "File " + filename + " has been received.";
    > }
    > } catch (Exception err) {
    > lblReply.Text = err.Message;
    > }
    > }
    > }
    >
    > "UJ" <> wrote in message
    > news:...
    >> How can I add a browse button to my asp.net page? What I need is a button
    >> they can press that will then let them select the file to upload to the
    >> server.
    >>
    >> And if anybody has any good code on how to upload a file to the server
    >> I'd appreciate that also.
    >>
    >> TIA - Jeffrey.
    >>
    >>

    >
    >
     
    UJ, Jun 9, 2005
    #3
  4. UJ

    UJ Guest

    Bob,
    That does work great. But is there any way to have more control over it? For
    instance our buttons look different and I'd like to have the button below
    the field instead of next to id.

    Thanks again.

    "UJ" <> wrote in message
    news:...
    > That's perfect. Thanks!
    >
    > "Bob Weiner" <> wrote in message
    > news:O$...
    >> Here is something simple and straightforward:
    >>
    >>
    >> On the client side:
    >> <INPUT id="File1" type="file" runat="server" name="FileUpload">
    >> <asp:button id="btnUpload" runat="server"></asp:button>
    >>
    >>
    >> On the Server:
    >>
    >> // these will be added automatically if you are using Visual Studio
    >> protected System.Web.UI.WebControls.Button btnUpload;
    >> protected System.Web.UI.HtmlControls.HtmlInputFile File1;
    >> protected System.Web.UI.WebControls.Label lblReply;
    >>
    >> this.btnUpload.Click += new System.EventHandler(this.btnUpload_Click);
    >> // end of automatically added lines
    >>
    >> // this function assumes InQueue is defined
    >> private void btnUpload_Click(object sender, System.EventArgs e) {
    >> if (File1.PostedFile.FileName == "") {
    >> lblReply.Text = "You must specify the file first.";
    >> } else {
    >> string procID = DateTime.Now.ToString("MMddHHmm");
    >> string filename = InQueue + "NewUsers." + procID + ".xls";
    >> try {
    >> if (File.Exists(filename)) {
    >> lblReply.Text = "File already exists";
    >> } else {
    >> File1.PostedFile.SaveAs (filename);
    >> lblReply.Text = "File " + filename + " has been
    >> received.";
    >> }
    >> } catch (Exception err) {
    >> lblReply.Text = err.Message;
    >> }
    >> }
    >> }
    >>
    >> "UJ" <> wrote in message
    >> news:...
    >>> How can I add a browse button to my asp.net page? What I need is a
    >>> button they can press that will then let them select the file to upload
    >>> to the server.
    >>>
    >>> And if anybody has any good code on how to upload a file to the server
    >>> I'd appreciate that also.
    >>>
    >>> TIA - Jeffrey.
    >>>
    >>>

    >>
    >>

    >
    >
     
    UJ, Jun 9, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?Sm9l?=

    Extract filename from a filename typed by user

    =?Utf-8?B?Sm9l?=, Aug 23, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    1,029
    Travis Murray
    Aug 24, 2004
  2. Replies:
    1
    Views:
    1,486
    Roland de Ruiter
    Jun 15, 2006
  3. Ed
    Replies:
    10
    Views:
    45,939
    alok000707
    Jul 13, 2010
  4. Beauregard T. Shagnasty

    Re: filename.gif or filename.gif.jpg?

    Beauregard T. Shagnasty, May 30, 2008, in forum: HTML
    Replies:
    1
    Views:
    754
    Jonathan N. Little
    May 30, 2008
  5. Replies:
    0
    Views:
    211
Loading...

Share This Page