Solved

Question about Uploading Method

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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

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…
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…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

719 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