Solved

Question about Uploading Method

Posted on 2004-08-01
2
219 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
INNER JOIN ? 8 52
ASP Focus problem 3 71
ASP server side get value 15 46
Asp in server side with Mssql Server 7 4 33
Hello, all! I just recently started using Microsoft's IIS 7.5 within Windows 7, as I just downloaded and installed the 90 day trial of Windows 7. (Got to love Microsoft for allowing 90 days) The main reason for downloading and testing Windows 7 is t…
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/…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

749 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