Register Blogs FAQ Members List Social Groups Calendar Search Today's Posts Mark Forums Read

Go Back   DeveloperBarn Forums > Programming & Scripting > ASP Development

Sponsored Links

Discuss "Free ASP Upload (insert issue)" in the ASP Development forum.

ASP Development - Learn coding practices and tips to get the best out of your Active Server Pages (ASP). Visit the ASP Development forum for help with ASP/VBScript and ASP/JScript applications.


Closed Thread
 
LinkBack Thread Tools Display Modes
  #1  
Old July 22nd, 2008, 01:35 PM
Rebelle's Avatar
Barn Enthusiast
 
Join Date: Mar 2008
Posts: 295
Thanks: 54
Thanked 1 Time in 1 Post
Rep Power: 1
Rebelle is on a distinguished road
Default 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!
Sponsored Links
  #2  
Old July 23rd, 2008, 03:46 AM
richyrich's Avatar
Super Moderator
 
Join Date: Mar 2008
Location: Somewhere only we know...
Posts: 460
Thanks: 31
Thanked 43 Times in 43 Posts
Blog Entries: 1
Rep Power: 2
richyrich will become famous soon enoughrichyrich will become famous soon enough

Awards Showcase
JavaScript Classic ASP 
Total Awards: 2

Default

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.
The Following User Says Thank You to richyrich For This Useful Post:
Rebelle (July 23rd, 2008)
  #3  
Old July 23rd, 2008, 06:32 AM
mehere's Avatar
Super Sarcasm Mistress
 
Join Date: Mar 2008
Location: Wide Awake In Dreamland
Posts: 160
Thanks: 12
Thanked 29 Times in 27 Posts
Rep Power: 1
mehere will become famous soon enough

Awards Showcase
Microsoft SQL Server Classic ASP 
Total Awards: 2

Default

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:
Quality: The race for quality has no finish line- so technically, it's more like a death march.

Questions to Ponder:
What do you do when you see an endangered animal eating an endangered plant?

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  
Old July 31st, 2008, 09:36 AM
Rebelle's Avatar
Barn Enthusiast
 
Join Date: Mar 2008
Posts: 295
Thanks: 54
Thanked 1 Time in 1 Post
Rep Power: 1
Rebelle is on a distinguished road
Default

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

  DeveloperBarn Forums > Programming & Scripting > ASP Development

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

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 &amp; Rebelle ASP Development 3 April 17th, 2008 10:54 AM


All times are GMT -4. The time now is 09:15 AM.



Content Relevant URLs by vBSEO 3.2.0