File Upload Using ASP.NET VB

I noticed or I should say it was brought to my attention that although I show how to work with files, I never discussed uploading files.  Well here it is.

 

In its simplest form, uploading a file using asp.net VB is quite simple. The following 2 links show how it is done:

 

http://msdn.microsoft.com/en-us/library/aa478971.aspx

http://support.microsoft.com/kb/323245

 

This link covers size limitations and how to circumvent it and also other things like multiple file uploads, controlling file types, and other issues.

 

On another note, if you want to see the code right here and now, just see below (this is how I do it):

 

In the display page we have the following:

 

<font face=”verdana”>Select File to Upload:</font>

<input id=”uploadedFile” type=”file” accept=”" runat=”server” />

<input type=”button” id=”upload” value=”Upload” onserverclick=”Page_Load” runat=”server” />

 

In the code behind we do this:

Make sure you import:

Imports System.IO 

Then button_click() you have:

If Not (uploadedFile.PostedFile Is Nothing) Then

Try

Dim postedFile As HttpPostedFile = uploadedFile.PostedFile

Dim filename As String = Path.GetFileName(postedFile.FileName)

Dim fileExt As String = Path.GetExtension(postedFile.FileName)

Dim contentType As String = postedFile.ContentType

Dim contentLength As Integer = postedFile.ContentLength

Dim qryString As String = Request.QueryString(“dir”)

If fileuploaded = True Then

message.Text = “The file was renamed and uploaded successfully.”

postedFile.SaveAs(currentDir & directorySeparatorChar.ToString() & filename)

End If

Catch ex As Exception

message.Text = “Failed uploading file. Please use feedback form.”

End Try

End If

 

The qryString variable would be the directory you are saving the files to.

This is how I did my qryString;

‘Find out the server name

 Dim srvrnm As String = Server.MachineName

‘Set up root directory. Doesn’t matter if live of dev, will know by above statement

Dim currentDir As String = “\\” & srvrnm & “\folder\folder”

 

To rename the files you would add the following code after the postedFile.saveAs above:

File.Move(currentDir & directorySeparatorChar.ToString() & filename, currentDir & directorySeparatorChar.ToString() & FilereName & fileExt)

 

A couple of last things, the ASPNET user needs to have permissions to write to the folder you are uploading to. Also, if you upload a file that is already there, it will not overwrite, it will throw an error. You have to try catch that error. You also have to add enctype=”multipart/form-data” to the form tag to allow file to be uploaded.

 

To allow for larger files than 4Mg you need to add the following code to the web.config file located in the application you are working on (add it inside the <system.web> node):

<httpRuntime

executionTimeout=”90″

maxRequestLength=”4096″ ß This would be changed to indicate size of file in Kb

useFullyQualifiedRedirectUrl=”false”

minFreeThreads=”8″

minLocalRequestFreeThreads=”4″

appRequestQueueLimit=”100″ />

 

That should work for you. If there are any issues or questions, post them and I will do my best to get back to you with an answer or a solution. Thank you.