upload file restrict

Discussion in 'Classic ASP' started by maw, Jan 22, 2004.

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

    maw

    Hi, I am trying to allow users to upload only some types of images under a certain size

    I can get the basic upload working fine using

    <%
    Set Upload = Server.CreateObject("Persits.Upload.1")
    Upload.SetMaxSize 5000, True
    Count = upload.savevirtual("/test/upload")
    %> </font id="green">

    I found this guide for restricting the file size and type digitalcolony.com/01/upload/

    so I am using the shared code -

    <%
    intMaxFileSize = 8000
    strUploadFolder = "c:\uploadFolder"

    Function isFileSizeOK(bytes)
    '=== restrict file byte size
    byteMAX = intMaxFileSize
    If bytes > byteMAX Then
    isFileSizeOK = FALSE
    Else
    isFileSizeOK = TRUE
    End If
    End Function

    Function isValidFile(filename)
    '=== define what file types you will permit to upload
    fileExtension = lcase(right(filename,4))
    select case fileExtension
    case ".gif",".jpg",".png","jpeg"
    isValidFile = TRUE
    case else
    isValidFile = FALSE
    end select
    End Function</font id="green">

    with the aspupload code

    <%
    Sub uploadPersists
    Set up = Server.CreateObject("Persits.Upload.1")
    up.OverwriteFiles = TRUE
    up.SetMaxSize intMaxFileSize
    up.Save uploadFolder
    For Each File in up.Files
    fileName = File.ExtractFileName
    If isValidFile(fileName) Then
    If isFileSizeOK(File.OriginalSize) Then
    strUploadStatus2 = "File [" & filename & "] Uploaded Successfully! "
    Else
    strUploadStatus2 = "ERROR: File Too Large: " & fileName & " (" & File.OriginalSize & " bytes)"
    File.Delete
    End If
    Else
    File.Delete
    strUploadStatus2 = "ERROR: This File Type is restricted from uploading: " & fileName
    End If
    Next
    End Sub
    %> </font id="green">

    If I use it as it is (obviously adding my folder) I get the SaveVirtual error. When I change it to virtual I get the following error - Server.MapPath() error 'ASP 0171 : 80004005'

    I have tried removing strUploadFolder and just using

    up.Savevirtual ("/test/upload") </font id="green">

    Basically this doesnt throw up any errors - it just doesnt do anything. I have played around with the code a bit and can get it to upload but it simply disregards the file type or size restrictions - no errors it just doesnt seem to be processing it.

    Anyone have any ideas why I cant get this working - I imagine I am missing something totally obvious. Or does have anyone managed to so a similar thing - and if so can I steal your knowledge?

    Thanks
     
  2. Hi mate,

    I've been trying to do the same thing! Used some of the code against what I already had, and noticed a mistake... strUploadFolder later becomes UploadFolder which isn't going to work. Also, if the permissions aren't set up on the folder, then the file.delete won't work.

    Anyway, the following code works on mine...
    ----
    <%

    intMaxFileSize = 524288
    strUploadFolder = "D:\webspace\xxxxxxxxxxxx_some_folder"

    Function isFileSizeOK(bytes)
    '=== restrict file byte size
    byteMAX = intMaxFileSize
    If bytes > byteMAX Then
    isFileSizeOK = FALSE
    Else
    isFileSizeOK = TRUE
    End If
    End Function

    Function isValidFile(filename)
    '=== define what file types you will permit to upload
    fileExtension = lcase(right(filename,4))
    select case fileExtension
    case ".gif",".jpg",".png","jpeg"
    isValidFile = TRUE
    case else
    isValidFile = FALSE
    end select
    End Function

    '---------------------
    Set up = Server.CreateObject("Persits.Upload.1")
    up.OverwriteFiles = TRUE
    up.SetMaxSize intMaxFileSize
    up.Save strUploadFolder
    For Each File in up.Files
    fileName = File.ExtractFileName
    If isValidFile(fileName) Then
    If isFileSizeOK(File.OriginalSize) Then
    strUploadStatus2 = "File [" & filename & "] Uploaded Successfully! "
    Else
    'File.Delete
    strUploadStatus2 = "ERROR: File Too Large: " & fileName & " (" & File.OriginalSize & " bytes)"
    End If
    Else
    'File.Delete
    strUploadStatus2 = "ERROR: This File Type is restricted from uploading: " & fileName
    End If
    Next

    %>


    <HTML>

    <CENTER>


    <p></p>

    <%=strUploadStatus2%>

    </CENTER>
    </BODY>
    </HTML>
     
  3. Bruce

    Bruce DiscountASP.NET Staff

    This error is usually caused by insufficient permission. By default, your account is set up to have Anonymous change permission. However, if

    1) you have Frontpage extension enabled

    or

    2) you changed permission in the permission manager

    The permission will change.

    Check the permission make sure you have anonymous write.

    In addition, .remove function is prohibited as suggested by Persits.



    quote:Originally posted by maw


    Hi, I am trying to allow users to upload only some types of images under a certain size

    I can get the basic upload working fine using

    <%
    Set Upload = Server.CreateObject("Persits.Upload.1")
    Upload.SetMaxSize 5000, True
    Count = upload.savevirtual("/test/upload")
    %> </font id="green">

    I found this guide for restricting the file size and type digitalcolony.com/01/upload/

    so I am using the shared code -

    <%
    intMaxFileSize = 8000
    strUploadFolder = "c:\uploadFolder"

    Function isFileSizeOK(bytes)
    '=== restrict file byte size
    byteMAX = intMaxFileSize
    If bytes > byteMAX Then
    isFileSizeOK = FALSE
    Else
    isFileSizeOK = TRUE
    End If
    End Function

    Function isValidFile(filename)
    '=== define what file types you will permit to upload
    fileExtension = lcase(right(filename,4))
    select case fileExtension
    case ".gif",".jpg",".png","jpeg"
    isValidFile = TRUE
    case else
    isValidFile = FALSE
    end select
    End Function</font id="green">

    with the aspupload code

    <%
    Sub uploadPersists
    Set up = Server.CreateObject("Persits.Upload.1")
    up.OverwriteFiles = TRUE
    up.SetMaxSize intMaxFileSize
    up.Save uploadFolder
    For Each File in up.Files
    fileName = File.ExtractFileName
    If isValidFile(fileName) Then
    If isFileSizeOK(File.OriginalSize) Then
    strUploadStatus2 = "File [" & filename & "] Uploaded Successfully! "
    Else
    strUploadStatus2 = "ERROR: File Too Large: " & fileName & " (" & File.OriginalSize & " bytes)"
    File.Delete
    End If
    Else
    File.Delete
    strUploadStatus2 = "ERROR: This File Type is restricted from uploading: " & fileName
    End If
    Next
    End Sub
    %> </font id="green">

    If I use it as it is (obviously adding my folder) I get the SaveVirtual error. When I change it to virtual I get the following error - Server.MapPath() error 'ASP 0171 : 80004005'

    I have tried removing strUploadFolder and just using

    up.Savevirtual ("/test/upload") </font id="green">

    Basically this doesnt throw up any errors - it just doesnt do anything. I have played around with the code a bit and can get it to upload but it simply disregards the file type or size restrictions - no errors it just doesnt seem to be processing it.

    Anyone have any ideas why I cant get this working - I imagine I am missing something totally obvious. Or does have anyone managed to so a similar thing - and if so can I steal your knowledge?

    Thanks

    </blockquote id="quote"></font id="quote">
     
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