having trouble getting a fileupload control to work

Discussion in 'ASP.NET / ASP.NET Core' started by wisemx, Jan 7, 2008.

Thread Status:
Threads that have been inactive for 5 years or longer are closed to further replies. Please start a new thread.
  1. Take a look at this ASP.NET Picture Album project:
    http://www.beansoftware.com/ASP.NET-Tutorials/Picture-Album.aspx

    You probably don't need the data sections but look how it grabs the file and path:

     
  2. Hi ASP epxerts

    I'm having trouble getting a fileupload control to work. Here is the code connected to the upload button:

    <script runat="server">

    Sub btnAdd_Click(ByVal sender As Object, ByVal e As EventArgs)
    If (photoUpload.HasFile) Then
    If (CheckFileType(photoUpload.FileName)) Then
    Dim filePath As String = "~\family\Photos" &amp; photoUpload.FileName
    photoUpload.SaveAs(MapPath(filePath))
    End If
    End If
    End Sub

    Function CheckFileType(ByVal fileName As String) As Boolean
    Dim ext As String = Path.GetExtension(fileName)
    Select Case ext.ToLower
    Case ".gif"
    Return True
    Case ".png"
    Return True
    Case ".jpg"
    Return True
    Case ".jpeg"
    Return True
    Case Else
    Return False
    End Select
    End Function

    </script>

    where photoUpload is the ID of the fileupload control. Do I need to set the NTFS permissions of the ~\family\Photos directory?

    Many thanks in advance for any tips and suggestions.

    Happy New Year!

    Kurt

    Post Edited By Moderator (mjp [DASP]) : 1/7/2008 8:16:34 PM GMT
     
  3. Thanks for the link... I've attempted to use the same approach as outlined in your post. Still having trouble though... can't even get it to work on my developer machine so clearly have to put a little more thought into this... K
     
  4. Bruce

    Bruce DiscountASP.NET Staff

    well.. what was the problem? Do you get any error or the file ended up in a different location?

    Bruce

    DiscountASP.NET
    www.DiscountASP.NET
     
  5. Hi Bruce


    No error or any indication that there is a problem (not even when I use trace). I can't find the uploaded file anywhere else... most peculiar!


    Kurt
     
  6. Bruce

    Bruce DiscountASP.NET Staff

    weird!!!!


    If you didn't get any error, either your app trapped the error or the file got written to some location. I recommend try hardcode the path in your app and see if it works. If it does, then we can focus of the path those variable produces.


    Bruce

    DiscountASP.NET
    www.DiscountASP.NET
     
  7. Good catch.

    The Protected keyword places your sub, and any classes, in a more secure mode.
    Classes declared will only have access to their own member classes.

    In ASP.NET this is part of the script securities.
     
  8. Hi Bruce et al.

    Apparently the problem was a simple mistake of not starting my handling script with 'protected', i.e.:

    Protected Sub btnAdd_Click(ByVal sender As Object, ByVal e As EventArgs)

    as opposed to

    Sub btnAdd_Click(ByVal sender As Object, ByVal e As EventArgs)

    Can anyone explain why this is so important?

    Thanks, Kurt
     
  9. In case anyone is interested I added a bit more code so that a thumbnail is automatically created in a different folder (I've built a very basic family album). The thumbnail is 200 pixels wide and keeps the same aspect ratio as the original image.


    <%@ Import Namespace="System.Drawing" %>


    Protected Sub btnAdd_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnAdd.Click


    If photoUpload.HasFile Then


    If (CheckFileType(photoUpload.FileName)) Then


    Dim filePath As String = Request.PhysicalApplicationPath &amp; "family\Photos\" &amp; photoUpload.FileName


    'Dim filePath As String = "C:\Users\Bertie\Desktop\" &amp; photoUpload.FileName


    photoUpload.SaveAs(filePath)





    'Create thumbnail from uploaded image


    Dim thumbnailFilename As String = Request.PhysicalApplicationPath &amp; "family\Photos\Thumbnails\th_" &amp; photoUpload.FileName


    Dim originalImage = New Bitmap(filePath)


    Dim thumbnailImage = originalImage.GetThumbnailImage(200, originalImage.Height * 200 / originalImage.Width, Nothing, System.IntPtr.Zero)


    thumbnailImage.Save(thumbnailFilename)


    End If


    End If


    End Sub
     
Thread Status:
Threads that have been inactive for 5 years or longer are closed to further replies. Please start a new thread.

Share This Page