Closed Thread
Results 1 to 4 of 4

Thread: Free ASP Upload (insert issue)

  1. #1
    Barn Loyal Rebelle will become famous soon enough Rebelle's Avatar
    Join Date
    Mar 2008
    Posts
    868
    Rep Power
    3

    Free ASP Upload (insert issue)

    Hi,

    I'm having an issue understanding completely how to handle the file names fields...I have 2 on the form. I want to only allow 2 files for each form...sometimes there may be only 1 attachment.

    On my form I have the following:
    Code:
    <tr>
        <td>File 1: <input name="attach1" type="file" size=35><br></td>
        <td>File 2: <input name="attach2" type="file" size=35><br>
        </td>
        </tr>
    
    Here is where the problem is.....
    1) If I select 1 file to attach and submit the form, it gets uploaded to the server correctly but its filename gets added to the database fields (file1 & file2) instead of just file1.
    2) So I tested to see what happens when I select 2 files to attach, well, it uploads bot the server just fine, but it only inserts the 2nd filename selected in both fields (file1 & file2).

    Code:
    Code:
    function SaveFiles
        Dim Upload, fileName, fileSize, ks, i, fileKey
    
        Set Upload = New FreeASPUpload
        Upload.Save(uploadsDirVar)
    	strOriginator = Upload.Form("Originator")
    	strEQNumber = Upload.Form("EquipmentNumber")
    	strPartNo = Upload.Form("PartNo")
    	strEQDesc = Upload.Form("EquipDescription")
    	strFuncLoc = Upload.Form("NCRFuncLoc")
    	strNCRStatus = Upload.Form("NCRStatus")
    	strDispStatement = Upload.Form("Comments")
    	
    
    
    for each fileKey in Upload.UploadedFiles.keys
    	strFile1 = Upload.UploadedFiles(fileKey).FileName
    	strFile2 = Upload.UploadedFiles(fileKey).FileName
    	
    next
    if strFile <> "" then
    	set oFSO = Server.CreateObject("Scripting.FileSystemObject")
    	if oFSO.FileExists(uploadsDirVar & "/" & strFile) then
    		oFSO.MoveFile uploadsDirVar & "\" & strFile, Server.MapPath("/Files") & "\" & strFile
    	end if
    	set oFSO = nothing
    end if
    
    Set rs = Server.CreateObject ("ADODB.Recordset")
    strSQL = "Insert into tblTestNCR (Originator,Equipment,ManPartNo,EQDescription,District,Status,DiscStatement,File1,File2) values ('" & strOriginator & "','" & strEQNumber & "', '" & strPartNo & "', '" & strEQDesc & "', '" & strFuncLoc & "', '" & strNCRStatus & "', '" & strDispStatement & "', '" & strFile1 & "','" & strFile2 & "')"
    rs.Open strSQL, conn,3,1
    Conn.Close
    Set Conn = Nothing	
    
    
    	' If something fails inside the script, but the exception is handled
    	If Err.Number<>0 then Exit function
    
        SaveFiles = ""
        ks = Upload.UploadedFiles.keys
        if (UBound(ks) <> -1) then
            SaveFiles = "<B>Files uploaded:</B> "
            for each fileKey in Upload.UploadedFiles.keys
                SaveFiles = SaveFiles & Upload.UploadedFiles(fileKey).FileName & " (" & Upload.UploadedFiles(fileKey).Length & "B) "
            next
        else
            SaveFiles = "The file name specified in the upload form does not correspond to a valid file in the system."
        end if
    end function
    
    Thanks for your help!

  2. #2
    Administrator richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich's Avatar
    Join Date
    Mar 2008
    Location
    Somewhere only we know...
    Posts
    1,724
    Blog Entries
    10
    Rep Power
    11

    You are setting the variables strfile1 and strfile2 to the same value because they're inside a loop
    Code:
    for each fileKey in Upload.UploadedFiles.keys
        strFile1 = Upload.UploadedFiles(fileKey).FileName
        strFile2 = Upload.UploadedFiles(fileKey).FileName
    next
    
    I'm not sure exactly how FreeASPUpload works but you need to grab the filenames exclusively. Something like:-
    Code:
    strFile1 = Upload.UploadedFiles(0).FileName
    strFile2 = Upload.UploadedFiles(1).FileName
    
    I think you can then check the filename values.
    Code:
    if strFile1 = "" then
       don't save in db
    else
       save in db
    end if
    
    Hope that helps.

  3. #3
    Super Sarcasm Mistress mehere is a glorious beacon of light mehere is a glorious beacon of light mehere is a glorious beacon of light mehere is a glorious beacon of light mehere is a glorious beacon of light mehere's Avatar
    Join Date
    Mar 2008
    Location
    Wide Awake In Dreamland
    Posts
    436
    Rep Power
    7

    as RR said, you're putting the files into both arrays, and you would overwrite them with the second file. try putting them into a comma delimited string. something like this:
    Code:
    for each fileKey in Upload.UploadedFiles.keys
    	strFile1 = strFile1 & Upload.UploadedFiles(fileKey).FileName & ","
    next
    strFile1 = left(strFile1,len(strFile1)-1) 'this will remove the trailing comma
    
    then to insert, do this:
    Code:
    Set rs = Server.CreateObject ("ADODB.Recordset")
    if inStr(strFile1,",") > 0 then
         arrFile = split(strFile1,",")
         blnArray = 1
    end if
    if blnArray = 1 then
         strSQL = "Insert into tblTestNCR (Originator,Equipment,ManPartNo,EQDescription,District,Status,DiscStatement,File1,File2) values ('" & strOriginator & "','" & strEQNumber & "', '" & strPartNo & "', '" & strEQDesc & "', '" & strFuncLoc & "', '" & strNCRStatus & "', '" & strDispStatement & "', '" & arrFile(0) & "','" & arrFile(1) & "')"
    else
         strSQL = "Insert into tblTestNCR (Originator,Equipment,ManPartNo,EQDescription,District,Status,DiscStatement,File1,File2) values ('" & strOriginator & "','" & strEQNumber & "', '" & strPartNo & "', '" & strEQDesc & "', '" & strFuncLoc & "', '" & strNCRStatus & "', '" & strDispStatement & "', '" & strFile1 & "','')"
    end if
    rs.Open strSQL, conn,3,1
    
    Quote of the Month:
    Leaders: Leaders are like eagles. We don't have either of them here.

    Questions to Ponder:
    Why do banks charge you a "non-sufficient funds fee" on money they already know you don't have?

    iif([sarcasm]=true,iif([you have to ask]=true,"didn't work","ha ha ha"),"not sarcasm")
    copyright © 2008 sbenj69

    Sarchasm: The gulf between the author of sarcastic wit and the person who doesn't get it.

  4. #4
    Barn Loyal Rebelle will become famous soon enough Rebelle's Avatar
    Join Date
    Mar 2008
    Posts
    868
    Rep Power
    3

    The sql insert was working...but i needed to handle something on my form to take care of if someone does not want to upload a file....it should just leave the file1 & file2 fields blank. So I added the if statement in blue and it works...but now if I do choose to upload a file, it uploads the file but its not writing the filename to the database. it inserts all the other fields on the form and uploads the file to the server but just not inserting the filename(s) to file1 & file2 fields. do you see why the lines in blue would break code to insert the filename?

    Code:
    if fileKey <> "" then	
    for each fileKey in Upload.UploadedFiles.keys
    strFile1 = strFile1 & Upload.UploadedFiles(fileKey).FileName & ","
    next
    strFile1 = left(strFile1,len(strFile1)-1) 'this will remove the trailing comma
    end if
    
    
    if strFile <> "" then
    	set oFSO = Server.CreateObject("Scripting.FileSystemObject")
    	if oFSO.FileExists(uploadsDirVar & "/" & strFile) then
    		oFSO.MoveFile uploadsDirVar & "\" & strFile, Server.MapPath("/Files") & "\" & strFile
    	end if
    	set oFSO = nothing
    end if
    
    THIS WAY WORKS:
    Code:
    for each fileKey in Upload.UploadedFiles.keys
    	strFile1 = strFile1 & Upload.UploadedFiles(fileKey).FileName & ","
    next
    if FileName <> "" then	
    	strFile1 = left(strFile1,len(strFile1)-1) 'this will remove the trailing comma
    end if
    
    Last edited by Rebelle; July 31st, 2008 at 10:39 AM. Reason: Got it! move the if statement line.

Closed Thread

Similar Threads

  1. Free ASP Upload Question
    By Rebelle in forum ASP Development
    Replies: 3
    Last Post: July 17th, 2008, 12:05 PM
  2. dynamically created dropdowns insert to sql
    By peebman2000 in forum .Net Development
    Replies: 14
    Last Post: May 19th, 2008, 11:00 AM
  3. Issue with ampersand &amp;
    By Rebelle in forum ASP Development
    Replies: 3
    Last Post: April 17th, 2008, 10:54 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

SEO by vBSEO