Link to home
Start Free TrialLog in
Avatar of Gemini532
Gemini532Flag for United States of America

asked on

How to store an array into a session variable

Hello Everyone,
How can I store the files that the user attaches to the application into an array, and then into a session variable?
Every time the user clicks this button:
<input type = "submit" value = "Save and Attach Another" name = "attachBtn"  id = "attachBtn">
a parth telling me where the file is located on the user's computer is saved in this: Upload.Form("file1")
But I would like to add it to an array, and then to a session variable.
Can you help?

Dim MyArray()

for i = 1 to ?!?!?!?
MyArray(i) =  Upload.Form("file1")
end

'Storing the array in the Session object.
Session("StoredArray") = MyArray
Avatar of SStory
SStory
Flag of United States of America image

Well, storing an array in a Session variable could be done with a delimiter between each item, however, I think Session variables use cookies if you are referring to ASP...thus the amount of space is really limited in that case.
Avatar of Gemini532

ASKER

I'm using ASP VBScript...
Sorry I really have never done this before and I don't know how to do it, but I do need to save an array into a session variable...
Can you show me how?
Are you using Persits.Upload object ?? If so, the following article would be useful to you: http://www.aspupload.com/manual_simple.html

<%
Dim iCount, iLoop, objUpload, objFile, arrFiles()

Set objUpload = Server.CreateObject("Persits.Upload.1")
iCount = objUpload.Save "c:\upload"

' --- ReDim array starting from 0 ---
ReDim arrFiles(iCount - 1)

iLoop = 0
For Each objFile in objUpload.Files
  arrFiles(iLoop) = objFile.Path
  iLoop = iLoop + 1
Next
%>
Try This,


Dim myArray()
Dim i As Integer

myArray() = Session("ArraySessionName")
i = Ubound(myArray)+1
Redim Preserve myArray(i)

myArray(i) = Upload.Form("file1")
Session("ArraySessionName") = myArray()

I'm using Free ASP upload
Session("fileName") =  Upload.Form("file1")

Dim fileName
fileName = Session("fileName")
This is my code, but it doesn't work... It saves just the last file, not all the files in an array...
>>sagacitysolutions I'll try your suggestion and let you know...

dim FilePath1, nextNo
if not rs.eof then
      FilePath1 = Session("fileName")
      Set fs=Server.CreateObject("Scripting.FileSystemObject")    
      nextId = rs("ProposalId")
      nextNo = CInt(nextId) + 1
      fileName = nextNo & "_" & fs.GetFileName(FilePath1)
      'response.write("<br>" & fileName & "<br>")
      set fs=nothing
end if

Session("fileName") = fileName
response.write Session("fileName")
>>hi sagacitysolutions,
I'm getting an error here:             myArray() = Session("fileName")

ERROR:
Microsoft VBScript runtime error '800a0009'

Subscript out of range

/angie/browse.asp, line 55
Try,
myArray = Session("fileName")

Exclude the parentheses after the array name
To copy to an array use the following

Dim aArray()
Dim i

Do while not rs .EOF
   i = i + 1
   Redim Preserve aArray(, i)
   aArray( i) =  rs.Fields("ProposalId")
    rs.movenext
Loop
remove the ,
 Redim Preserve aArray(, i)
 Redim Preserve aArray(i)
As I was trying to tell you. Session variables use cookies and I think you will have problems if this site is used much.

see:
    http://www.15seconds.com/issue/010307.htm

A more scalable solution is to keep track of a unique identifier, store whatever in the database and look it back up as needed.

Otherwise you've gotten a lot of good answers on how to do it, just not why you probably shouldn't do it.
The reason I need to use Session variable is that after I get the value in the Session variable, I need to call it from another page called proposals.asp where I save it to the database.  So now my code in browse.asp gives me no errors, but the problem is my code in "proposals.asp"
When I do response.write fileName, I get no value, and there's no value in the Attachments table in my database.  I need every attached file to show up in the database, with the proposalId in front
For example, if the user attaches  c://angie/text.doc            c://angie/cat.jpg          c://angie/MyCar.gif        and the proposalId   =150

In the Attachments table it should be:
FileId                                   FileName                                            ProposalId
AutoNo                                150_text.do                                                   150
AutoNo                                150_cat.jpg                                                   150
AutoNo                                150_MyCar.gif                                               150

'*************************this is the code in "browse.asp"****************
dim rs, nextId
set rs = Server.CreateObject("ADODB.Recordset")
userRequestId = "Select TOP 1 [Proposals].[ProposalId] from [Proposals] order by DateSubmitted DESC"
rs.open userRequestId, objConn

if not rs.eof then
      FilePath1 = Session("fileName")
      Set fs=Server.CreateObject("Scripting.FileSystemObject")    
                  
      fileName = rs("ProposalId") & "_" & fs.GetFileName(FilePath1)
      response.write("<br>" & fileName & "<br>")
      set fs=nothing
end if


Dim aArray()
Dim i

Do while not rs.EOF
   i = i + 1
   Redim Preserve aArray(i)
   aArray(i) =  rs.Fields("ProposalId") & "_" & fileName
    'Session("fileName") += aArray(i)
    rs.movenext
response.write aArray(i)
Loop
'*************************this is the code in "proposals.asp"***************

fileName = Session("fileName")
response.write fileName
'for i = 1 to Ubound(Session("fileName"))
            attachmentsSQL = "INSERT into [Attachments] (FileName, ProposalId) values ('" & fileName & "', '" & rs("ProposalId") & "')"
            objConn.Execute attachmentsSQL
'Next
I think hte code is browse.asp is the reason the code in proposals.asp does not work

Do while not rs.EOF
   i = i + 1
   Redim Preserve aArray(i)
   aArray(i) =  rs.Fields("ProposalId") & "_" & fileName
    Session("fileName") = aArray(i) + "|"
    rs.movenext
response.write Session("fileName")
Loop

Why use rs.EOF to get the proposalID, I already have it!!!!

What I need is an ARRAY of the fileName
I was thinking this code might work:
Dim myArray()
Dim i

myArray = Session("fileName")
i = Ubound(myArray)+1
Redim Preserve myArray(i)

myArray(i) = Upload.Form("file1")
Session("fileName") = myArray()

But if I do this:   myArray = Session("fileName")
I get Type mismatch
and if I do this  myArray() = Session("fileName")
I get script out of range error
hmm..

I work mostly in ASP.NET so it is hard for me to remember the specifics on file uploads.  when the user uploads the file using the typical html upload, I suppose your ASP file doesn't really know about it.  Is that true? If not then seems as soon as you get the file you'd write that to the database there.  (Seems you would have to know because this is POST, correct?

If so and you post the uploaded file to an ASP file, you should know the name and then be able to insert to the DB and no need arrays.

Otherwise, I am just missing it.

>>SStory
It's not quite this simple.
I'm using 3 files submit.asp which opens browse.asp and here I have ONLY one input button where the user selects the attachments and they get saved ONE BY ONE to a folder in the web server which has write permissions.
Then I close browse.asp and I'm in submit.asp where, I click SUBMIT and submit the rest of the elements in the page, textboxes, radio buttons, etc...
Then there's the last file, proposals.asp where I put the data from browse.asp and the data from submit.asp in the database, 2 different tables "Attachments and Proposals" which are connected by the proposalId   The Attachments table saves ONLY the file Names for the attachments, in this way:
In the Attachments table it should be:
FileId                                   FileName                                            ProposalId
AutoNo                                150_text.do                                                   150
AutoNo                                150_cat.jpg                                                   150
AutoNo                                150_MyCar.gif                                               150

The problem is that I have ONLY one input textbox in browse.asp called attach1, and somehow I have to get all 3 attachment names into the Attachment table.
What is happening right now, only the last ONE: 150_MyCar.gif  goes to the database.  The last attachment overrides the first 2...
That is the problem...
ASKER CERTIFIED SOLUTION
Avatar of sagacitysolutions
sagacitysolutions
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Gemini, Let me know if I am m,isunderstanding this
Ok.

Can't you separate each file by a delimiter (a comma a pipe or whatever) on one page, putting it into a session variable

Session("myfiles")=Session("myfiles") & "," & strNewFile

and then split it back out on the other page to write to a database?

This will work if you don't run out of space.

HTH,

Shane
Well the problem is the files are saved on THE DMZ in a folder with write permissions
That's not safe, is it?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you all.  All your suggestions were helpful...
What I'm doing is so difficult because so much of it depends on security and everything is new to me.
I've never done this before...