Solved

Question about Uploading Method

Posted on 2004-08-01
2
216 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
ID: 11690724
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
ID: 11692555

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I would like to start this tip/trick by saying Thank You, to all who said that this could not be done, as it forced me to make sure that it could be accomplished. :) To start, I want to make sure everyone understands the importance of utilizing p…
This demonstration started out as a follow up to some recently posted questions on the subject of logging in: http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28634665.html and http://www.experts-exchange.com/Programming/…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

912 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

21 Experts available now in Live!

Get 1:1 Help Now