![]() |
| |||||||
| Sponsored Links |
![]() | « Previous Thread | Next Thread » |
| | LinkBack | Thread Tools | Display Modes |
|
#1
| ||||
| ||||
| 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>
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
|
| Sponsored Links |
|
#2
| ||||
| ||||
| 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
Code: strFile1 = Upload.UploadedFiles(0).FileName strFile2 = Upload.UploadedFiles(1).FileName Code: if strFile1 = "" then don't save in db else save in db end if |
| The Following User Says Thank You to richyrich For This Useful Post: | ||
Rebelle (July 23rd, 2008) | ||
|
#3
| ||||
| ||||
| 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 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: Regret: It hurts to admit when you make mistakes - but when they're big enough, the pain only lasts a second. Questions to Ponder: Could it be that all those trick-or-treaters wearing sheets aren’t going as ghosts but as mattresses? iif([sarcasm]=true,iif([you have to ask]=true,"didn't work","ha ha ha"),"not sarcasm") copyright © 2008 sbenj69 |
| The Following User Says Thank You to mehere For This Useful Post: | ||
Rebelle (July 23rd, 2008) | ||
|
#4
| ||||
| ||||
| 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
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. |
![]() |
|
| Bookmarks |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Free ASP Upload Question | Rebelle | ASP Development | 3 | July 17th, 2008 12:05 PM |
| dynamically created dropdowns insert to sql | peebman2000 | .Net Development | 14 | May 19th, 2008 11:00 AM |
| Issue with ampersand & | Rebelle | ASP Development | 3 | April 17th, 2008 10:54 AM |