Click to See Complete Forum and Search --> : uploading files via web page [RESOLVED]
Muddy
Jan 5th, 2003, 09:58 AM
how can I implement the ability for the user to upload files (this user cannot FTP) via an asp.net web page? Something similar to the attachment feature on this forum might work.
Thanks for any help!
Edneeis
Jan 5th, 2003, 11:27 AM
Try the FileField control in the HTML section of the toolbox. You can use the SaveAs method.
Redth
Jan 5th, 2003, 12:15 PM
Try something like this:
'Only work with the files if in fact one has been posted
If filePicture.PostedFile.ContentLength > 0 Then
'Get the type, length, and name
strContentType = filePicture.PostedFile.ContentType
strFileName = filePicture.PostedFile.FileName
intContentLength = Convert.ToInt32(Math.Round(filePicture.PostedFile.ContentLength() / 1000, 0)) 'Convert from bytes to kbytes
'If the picture is a gif or Jpeg, continue
If strContentType = "image/pjpeg" Or strContentType = "image/gif" Or strContentType = "image/jpeg" Or strContentType = "image/jpg" Then
'Check to make sure picture isn't too big (in KB)
If intContentLength < 153 Then
'Save the Picture
filePicture.PostedFile.SaveAs("C:\InetPub\wwwroot\MySite\" & strFileName)
Else
'Too big of image
End If
Else
'Incorrect ContentTYpe
End If
Else
'No File Uploaded
End If
Your html would look like this:
<input Runat="server" ID="filePicture" Type="File">
Now that should give you a picture of what all you can do with uploads :) It's a simple script that if they uploaded a picture, and it's a image type, and the file is small enough, it saves it...
note: You could check the ending of the filename to check filetype, however the contenttype looks at the file and gets its real file type... so that nobody uploads anything malicious under a different file extension...
Muddy
Jan 5th, 2003, 03:23 PM
Thanks,
but why am I geting the following error?
Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
Source Error:
Line 25: Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Line 26: Dim strFileName As String
Line 27: strFileName = fileToUpload.PostedFile.FileName <-----error here
Line 28: fileToUpload.PostedFile.SaveAs(strFileName)
Line 29: End Sub
Muddy
Jan 5th, 2003, 08:39 PM
found my problem. had to manually edit the form tag info in the HTML to include:
encType:="multipart/form-data"
Thanks for everything!
Redth
Jan 5th, 2003, 10:46 PM
you got it!
actually, today i did the same thing... forgot about it.. i always seem to forget about it whenever i make a page using uploads :p
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.