Uploading Files


G

Guest

Hi,

I'm developing a web application where users can upload multiple files to
the website.
I'm using asp.net 2.0 with FileUpload control and can upload 1 image to the
server, but I would like to upload 5 images in total.

Can someone help me in writing a VB.NET part on how I can upload these
multiple files using the FileUpload control?

Dim UploadPath As String = Request.PhysicalApplicationPath + "\images\"

'Before attempting to save the file, verify that the FileUpload
Control contains a file.
If (FileUpload1.HasFile) Then

'Get the name of the file to upload.
Dim fileName As String = FileUpload1.FileName

'Get the extension of the upload file.
Dim extension As String = System.IO.Path.GetExtension(fileName)

'Allow only files with .JPEG or .GIF extensions
If (extension = ".jpeg") Or (extension = ".gif") Then


'Get the size in bytes of the file uploaded.
Dim FileSize As Integer = FileUpload1.PostedFile.ContentLength

'Allow only files less than 5,100,000 bytes (Approximately 5
MB) to be uploaded.
If (FileSize < 800000) Then

'Append the name of the uploaded file to the path.
UploadPath += FileUpload1.FileName

'Call the SaveAs method to save the uploaded file.
FileUpload1.PostedFile.SaveAs(UploadPath)

'Notify the user that the file has been uploaded
successfully.
FileUploadReport.Text = "<b>Bestand is opgeladen naar de
website vanaf: </b><br>" & _
FileUpload1.PostedFile.FileName & _
"Content Type " & FileUpload1.PostedFile.ContentType & _
"Length " & FileUpload1.PostedFile.ContentLength
SqlDataSource1.Insert()
Else
FileUploadReport.Text = "<b>Foto is niet opgeladen omdat
het bestand groter is dan 800K!</b><br>"
End If

Else
'Notify the user why their file was not uploaded.
FileUploadReport.Text = "<b>Bestand is niet opgeslagen, dit
omdat U niet het juiste formaat hebt geselecteerd!</b><br>" + _
"<b>Alleen het formaat .JPEG en .GIF
zijn toegelaten </b><br><br>"

End If
Else
FileUploadReport.Text = "<b>Gelieve een foto te selecteren
vooraleer U op de knop 'opladen' klikt</b><br>"
End If

End Sub

Here is my code :
 
Ad

Advertisements

B

Bruce Barker

you just put 5 fileuploads controls on the form, and for each one the user
specifies a file for, that file will be uploaded on the submit.

-- bruce (sqlwork.com)
 
G

Guest

Hi Bruce,

I put 5 FileUploads controls in my page, but as showed in my code, I only
bound the first FileUpload1 control and not the 4 others, this because I
can't find a way on how to bind them as well.
I looked on the net and saw some example's with a loop or For Each...Next
step, but I'm just starting with VB.NET and don't now how to bind the 4 other
controls in my procedure.

Can you help me?
Thanks,
B.
 
E

Edwin Knoppert

I don't think it's possible.
Due security risks the textboxes are cleared each time.
So on 1st submit, the others will be lost.
You *might* try semicolon (;) ?
 
G

Guest

Hi Edwin,

What do you mean with semicolon?

B.

Edwin Knoppert said:
I don't think it's possible.
Due security risks the textboxes are cleared each time.
So on 1st submit, the others will be lost.
You *might* try semicolon (;) ?
 
G

Guest

Hi Edwin & Bruce,

Find a solution by using the HttpFileCollection in my VB code behind file.

Example:

Dim UploadPath As String = Request.PhysicalApplicationPath + "\images\"

'Get the HttpFileCollection
Dim uploadedFiles As HttpFileCollection = Request.Files

'Get the extension of the upload file.
Dim extension As String = System.IO.Path.GetExtension(fileName)

'Allow only files with .JPEG or .GIF extensions
If (extension = ".jpeg") Or (extension = ".gif") Then


'Get the size in bytes of the file uploaded.
Dim FileSize As Integer = FileUpload1.PostedFile.ContentLength

'Allow only files less than 5,100,000 bytes (Approximately 5 MB)
to be uploaded.
If (FileSize < 800000) Then
Dim i As Integer = 0

Do Until i = uploadedFiles.Count
Dim userPostedFile As HttpPostedFile = uploadedFiles(i)

Try
If (userPostedFile.ContentLength > 0) Then
FileUploadReport.Text += "<u>File #" & (i + 1) &
"</u><br>"
FileUploadReport.Text += "File Content Type: " & _
userPostedFile.ContentType & "<br>"
FileUploadReport.Text += "File Size: " & _
userPostedFile.ContentLength & "kb<br>"
FileUploadReport.Text += "File Name: " & _
userPostedFile.FileName & "<br>"

userPostedFile.SaveAs(UploadPath & "\" & _
Path.GetFileName(userPostedFile.FileName))
SqlDataSource1.Insert()

End If
Catch ex As Exception
FileUploadReport.Text += "Error:<br>" & ex.Message
End Try
i += 1
Loop




End Sub
 
Ad

Advertisements

E

Edwin Knoppert

1) Please read posts better.
2) Have you considered using a better (nick)name?
 

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

Similar Threads


Top