Results 1 to 6 of 6

Thread: uploading files via web page [RESOLVED]

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Feb 2001
    Location
    Crossroads
    Posts
    3,046

    uploading files via web page [RESOLVED]

    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!
    Last edited by Muddy; Jan 5th, 2003 at 09:40 PM.

  2. #2
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Try the FileField control in the HTML section of the toolbox. You can use the SaveAs method.

  3. #3
    Fanatic Member Redth's Avatar
    Join Date
    May 2001
    Location
    Ontario, Canada
    Posts
    551
    Try something like this:
    VB Code:
    1. 'Only work with the files if in fact one has been posted
    2. If filePicture.PostedFile.ContentLength > 0 Then
    3.     'Get the type, length, and name
    4.     strContentType = filePicture.PostedFile.ContentType
    5.     strFileName = filePicture.PostedFile.FileName
    6.     intContentLength = Convert.ToInt32(Math.Round(filePicture.PostedFile.ContentLength() / 1000, 0))    'Convert from bytes to kbytes
    7.  
    8.     'If the picture is a gif or Jpeg, continue
    9.     If strContentType = "image/pjpeg" Or strContentType = "image/gif" Or strContentType = "image/jpeg" Or strContentType = "image/jpg" Then
    10.  
    11.         'Check to make sure picture isn't too big (in KB)
    12.         If intContentLength < 153 Then
    13.  
    14.             'Save the Picture
    15.             filePicture.PostedFile.SaveAs("C:\InetPub\wwwroot\MySite\" & strFileName)
    16.  
    17.         Else
    18.             'Too big of image
    19.         End If
    20.  
    21.     Else
    22.         'Incorrect ContentTYpe
    23.     End If
    24.  
    25. Else
    26.     'No File Uploaded
    27. End If

    Your html would look like this:
    VB Code:
    1. <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...

  4. #4

    Thread Starter
    PowerPoster
    Join Date
    Feb 2001
    Location
    Crossroads
    Posts
    3,046
    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

  5. #5

    Thread Starter
    PowerPoster
    Join Date
    Feb 2001
    Location
    Crossroads
    Posts
    3,046
    found my problem. had to manually edit the form tag info in the HTML to include:

    encType:="multipart/form-data"

    Thanks for everything!

  6. #6
    Fanatic Member Redth's Avatar
    Join Date
    May 2001
    Location
    Ontario, Canada
    Posts
    551
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width