Tuesday, May 12, 2009

Easy file upload from client to server using FileUpload

I needed a way to do a file upload from client to server without using FTP and found this easy solution using the FileUpload control.

Duh.

The control appears as a texbox with a Browse... button. The following puts the control on the page and sets the file to be uploaded to directory C:\\rptsys on the server.

<asp:FileUpload ID="FileUpload1" runat="server" filename ="C:\\rptsys\" />

Added a button to initiate the upload and a label to display the result:

<asp:Button ID="Button1" runat="server" Text="UploadFile" onclick="Button1_Click" /> </div> <asp:Label ID="Label1" runat="server" Text="Label"Width="247px"></asp:Label>

I put together a simple UI:


The Browse button opens up a full choose file dialog window.

The backend code:

protected void Button1_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile)
{
Label1.Text = "File Uploaded: " + FileUpload1.FileName;
FileUpload1.SaveAs(@"C:\temp\uploads\" +
FileUpload1.FileName);
}
else
{
Label1.Text = "
No File Uploaded.";
}
}

After the file is transmitted, I display a "success" message.

I would have liked to prepopuate the Browse textbox to the directory I know the file is in, but apparently, this cannot be done for security reasons.

Tuesday, May 5, 2009

Microsoft Bug Du Jour

Last Wednesday I was called over by a coworker and informed that my Asp.Net 3.5 app would not run on a 64 Bit PC running Vista.

Where the drop down menu should have been displaying a list of reports, there was a very empty, very white, rectangular box.

Took a look at the View Source to see what was going on. The menu was there and seemed ok.

Then I find out that the machine is running Explorer 8. Click on Compatibility View in the menu and site displays correctly.