Solved

Question about Uploading Method

Posted on 2004-08-01
2
215 Views
Last Modified: 2012-08-14

I have created a script below that allows the user to upload a file. That script then takes the path of the file and strips it to the name of the file and stores it into the database. Moreover, it should also upload it to the directory as well. If you look at the source code, I have set several conditions that prevent the user from uploading anything but image files like "gif," "jpg," and "png." Of course, if it doesn't meet the conditions then the submit button won't appear. If so, the user could hit confirm to submit the image for upload.

The problem with the script is, it sets the name of the file within the database. However, the file doesn't get uploaded to the directory. Can someone tell me what's wrong with my script? Why it isn't uploading my file? Thanks.

<%
      Set Upload = Server.CreateObject("Persits.Upload.1")
      
      Upload.SaveToMemory

    Upload.OverwriteFiles = False

      'checks for gif and jpg files, will reject if anything else
    For Each File in Upload.Files
    Ext = UCase(Right(File.Path, 3))
    If Not(Ext = "JPG" Or Ext = "GIF" Or Ext = "PNG") Then
      Response.Write("<br /><strong><font color=""red"">*ERROR:</font> File " & File.Path & " is invalid type.<br></strong>")
      Response.Write("<strong>Only jpg, gif, or png images (*.jpg, *.gif, and *.png) files can be uploaded here.<br /><br /></strong>")
      
      ELSEIf Upload.Form("pictureURL") <> "" Then
      Response.Write("<strong><font color=""red"">ERROR:</font> Please click back and submit an image. Thankyou!</strong>")
      
      Else
      
      ' *****************************************************************************************************************
      ' Set file size condition.
      'Upload.SetMaxSize 80000, False
      'If Upload.SetMaxSize > 80000 Then
      'Response.Write "<strong><font color=""red"">*ERROR: </font>Your file is over 80KB in size. Please go back<br></strong>"
      'Response.Write "<strong>and submit an image smaller than 80KB.<br></strong>"
      'Else
      
      ' The file uploaded by the user will be stored into the session id. It will then
      ' be parsed and stored into the database.
      
        Upload.Save("C:\Inetpub\wwwroot\sm\eswebadmin\webimages\") & File.ExtractFileName
       File.Copy("C:\Inetpub\wwwroot\sm\eswebadmin\webimages\") & Session("directory") & "\" & File.ExtractFileName
       'File
      
       Session("pictureURL") = Upload.Form("pictureURL")
      
      'End If

      'set web-friendly file names here?
      'NewName = Session.SessionID

      'For Each File in Upload.Files
         'File.Copy "c:\upload\" & NewName & File.ext
         'File.Delete
         'Response.Write "New name: " & NewName & File.ext & "<BR>"
      'Next
      ' *****************************************************************************************************************
      'File.SaveAs "C:\Inetpub\wwwroot\eswebadmin\webimages\" & File.ExtractFileName
      
      set Session("PictureURL") = nothing 'clean up stray variables
      Session("PictureURL") = File.ExtractFileName
%>  
     <form action="<%=MM_editAction%>" method="POST" name="addimagedatabase">
     <input type="hidden" name="pictureurl" value="<%= Session("pictureURL") %>" />
     <input type="hidden" name="storyid" value="<%=Upload.Form("storyid")%>" />
     <input type="hidden" name="picturenotes" value="<%=Upload.Form("picturenotes")%>" />
     <input type="hidden" name="rank" value="<%=Upload.Form("rank")%>" />
<%
     Response.Write("     <b>Your upload needs to be confirmed.</b>")
     Response.Write("     <p><input type=""submit"" name=""Submit"" value=""Click to Complete Upload"" /></p>")
      
       End If
       Next
%>
       
     <input type="hidden" name="MM_insert" value="addimagedatabase">
     </form>
0
Comment
Question by:onL0oker
2 Comments
 
LVL 6

Accepted Solution

by:
casstd earned 50 total points
Comment Utility
Hi,

    After looking through your code.

1. You have not wrriten any code to create the session("Directory") folder.
2. It may be due to permission is not given to the folder to read and write.


Set Upload = Server.CreateObject("Persits.Upload.1")
uploaddir = "DOCS"                              'Write your foldername here i.e rootfolder\Docs in this case                  
UPLOADDIRECTORY = Server.MapPath(uploaddir)                  
Upload.OverwriteFiles = false
Upload.SaveToMemory
                  
Count = Upload.Save(UPLOADDIRECTORY)     ' we usally use this property to update db if count is > 0. I.e file is stoed in directory
      
   For Each File in Upload.Files            
      Filename = File.ExtractFilename
      Ext = UCase(Right(File.Path, 3))
      size = File.size

   If Not(Ext = "JPG" Or Ext = "GIF" Or Ext = "PNG") Then
         
       Response.write ("<script language=""javascript"">")                    'Better to user Javascript
      Response.write ("{alert(""Only jpg, gif, or png images (*.jpg, *.gif, and *.png) files can be uploaded here."")}")
                Response.write "javascript:history.back();"
                 response.write ("</script>")
    ' Response.Write("<br /><strong><font color=""red"">*ERROR:</font> File " & File.Path & " is invalid type.<br></strong>")
     'Response.Write("<strong>Only jpg, gif, or png images (*.jpg, *.gif, and *.png) files can be uploaded here.<br /><br /></strong>")
   else
      
                     'create your session directory here. And move the file from the Webpages folder to the new folder you have created below.

                SET MyFileObject = Server.CreateObject("Scripting.FileSystemObject")
      if MyFileObject.FolderExists(UPLOADDIRECTORY & "\" & Session("Directory") & "\") = true then

      else                              
              Set newfolder = MyFileObject.CreateFolder(UPLOADDIRECTORY & "\" & session("Directory") & "\")
                              
      END IF
                     
      Call MyFileObject.MoveFile(File.Path, UPLOADDIRECTORY & "\" & session("Directory") & "\" & Filename)
      Set MyFileObject = nothing

   

     
     
      End If
      Next

       Now update the database if count is greater than 0 otherwise prompt them the file upload is unsuccesssful.

if count > 0 then

             'Update your db here.

else

             'Javscript prompt fileupload unsuccessful and go back to the form.
end if
   
0
 

Author Comment

by:onL0oker
Comment Utility

The condition that checks for the file type works. But when I submit an image that meets the condition, it doesn't allow it to upload. In addition to uploading an image to the folder, it suppose to copy the name of the file to the database as well.

<%
      Set Upload = Server.CreateObject("Persits.Upload.1")
      
      uploaddir = "c:\Inetpub\wwwroot\eswebadmin\webimages\"
      UPLOADDIRECTORY = Server.MapPath(uploaddir)
      Upload.SaveToMemory
      Upload.OverwriteFiles = False

      'checks for gif and jpg files, will reject if anything else
      For Each File in Upload.Files
      Filename = File.ExtractFilename
      Ext = UCase(Right(File.Path, 3))
      size = File.size
      
      If NOT(Ext = "JPG" Or Ext = "GIF" Or Ext = "PNG") Then
      'Response.Write("<strong><br /><font color=""red"">*ERROR:</font> File " & File.Path & " is invalid type.<br></strong>")
      'Response.Write("<strong>Only jpg or gif images (*.jpg, *.gif, and *.png) files can be uploaded here.<br /><br /></strong>")
       Response.write ("<script language=""javascript"">")                    'Better to user Javascript
     Response.write ("{alert(""Only jpg, gif, or png images (*.jpg, *.gif, and *.png) files can be uploaded here."")}")
     Response.write "javascript:history.back();"
     Response.write ("</script>")
 
      Else
      
      Set MyFileObject = Server.CreateObject("Scripting.FileSystemObject")
      If MyFileObject.FolderExists(UploadDirectory & "\" & Session("Directory") & "\") = true then
'Count =
      Upload.Save("c:\Inetpub\wwwroot\eswebadmin\webimages\") & File.ExtractFileName
      File.Copy("c:\Inetpub\wwwroot\eswebadmin\webimages\") & Session("directory") & "\" & File.ExtractFileName
'response.write(Count & " file uploaded!")
      
'Session("PictureURL") = File.ExtractFileName
      else                        
      Set newfolder = MyFileObject.CreateFolder(UPLOADDIRECTORY & "\" & session("Directory") & "\")
                         
     END IF
                     
     Call MyFileObject.MoveFile(File.Path, UPLOADDIRECTORY & "\" & session("Directory") & "\" & Filename)
     Set MyFileObject = nothing

      End If
      Next
%>  
     <form action="<%=MM_editAction%>" method="POST" name="addimagedatabase">
     <input type="hidden" name="pictureurl" value="<%= Session("pictureURL") %>" />
     <input type="hidden" name="storyid" value="<%=Upload.Form("storyid")%>" />
     <input type="hidden" name="picturenotes" value="<%=Upload.Form("picturenotes")%>" />
     <input type="hidden" name="rank" value="<%=Upload.Form("rank")%>" />
<%
      If count > 0 then

     Response.Write("     <b>Your upload needs to be confirmed.</b>")
     Response.Write("     <p><input type=""submit"" name=""Submit"" value=""Click to Complete Upload"" /></p>")
      else
       Response.write ("<script language=""javascript"">")                    'Better to user Javascript
     Response.write ("{alert(""Please submit a valid image. Thankyou!"")}")
     Response.write "javascript:history.back();"
     Response.write ("</script>")
       'response.write("<br />Please go back and submit a valid image. Thankyou!")
      End If
'End If
'Next
%>
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

I recently decide that I needed a way to make my pages scream on the net.   While searching around how I can accomplish this I stumbled across a great article that stated "minimize the server requests." I got to thinking, hey, I use more than one…
I have helped a lot of people on EE with their coding sources and have enjoyed near about every minute of it. Sometimes it can get a little tedious but it is always a challenge and the one thing that I always say is:  The Exchange of information …
This video discusses moving either the default database or any database to a new volume.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now