?
Solved

Question about Uploading Method

Posted on 2004-08-01
2
Medium Priority
?
222 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 6

Accepted Solution

by:
casstd earned 200 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

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 informatio…
I was asked about the differences between classic ASP and ASP.NET, so let me put them down here, for reference: Let's make the introductions... Classic ASP was launched by Microsoft in 1998 and dynamically generate web pages upon user interact…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

764 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