By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,805 Members | 1,056 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,805 IT Pros & Developers. It's quick & easy.

Browse for a filename?

P: n/a
UJ
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.
Nov 19 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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" <fr**@nowhere.com> wrote in message
news:uM**************@TK2MSFTNGP09.phx.gbl...
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.

Nov 19 '05 #2

P: n/a
UJ
That's perfect. Thanks!

"Bob Weiner" <bo*@engr.uconn.edu> wrote in message
news:O$**************@TK2MSFTNGP15.phx.gbl...
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" <fr**@nowhere.com> wrote in message
news:uM**************@TK2MSFTNGP09.phx.gbl...
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.


Nov 19 '05 #3

P: n/a
UJ
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" <fr**@nowhere.com> wrote in message
news:eZ**************@TK2MSFTNGP09.phx.gbl...
That's perfect. Thanks!

"Bob Weiner" <bo*@engr.uconn.edu> wrote in message
news:O$**************@TK2MSFTNGP15.phx.gbl...
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" <fr**@nowhere.com> wrote in message
news:uM**************@TK2MSFTNGP09.phx.gbl...
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.



Nov 19 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.