Browse for a filename?

U

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.
 
B

Bob Weiner

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;
}
}
}
 
U

UJ

That's perfect. Thanks!

Bob Weiner said:
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;
}
}
}
 
U

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top