Solved

How to store an array into a session variable

Posted on 2006-07-17
22
1,679 Views
Last Modified: 2008-01-09
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
0
Comment
Question by:Gemini532
  • 10
  • 5
  • 4
  • +2
22 Comments
 
LVL 25

Expert Comment

by:SStory
ID: 17125662
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.
0
 

Author Comment

by:Gemini532
ID: 17125675
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?
0
 
LVL 9

Expert Comment

by:blandyuk
ID: 17125690
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
%>
0
 
LVL 8

Expert Comment

by:sagacitysolutions
ID: 17125704
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()

0
 

Author Comment

by:Gemini532
ID: 17125770
I'm using Free ASP upload
0
 

Author Comment

by:Gemini532
ID: 17125790
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")
0
 

Author Comment

by:Gemini532
ID: 17125824
>>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
0
 
LVL 8

Expert Comment

by:sagacitysolutions
ID: 17125952
Try,
myArray = Session("fileName")

Exclude the parentheses after the array name
0
 
LVL 20

Expert Comment

by:hes
ID: 17126281
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
0
 
LVL 20

Expert Comment

by:hes
ID: 17126286
remove the ,
 Redim Preserve aArray(, i)
 Redim Preserve aArray(i)
0
 
LVL 25

Expert Comment

by:SStory
ID: 17129736
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.
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 

Author Comment

by:Gemini532
ID: 17130473
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
0
 

Author Comment

by:Gemini532
ID: 17131286
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
0
 

Author Comment

by:Gemini532
ID: 17131327
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
0
 
LVL 25

Expert Comment

by:SStory
ID: 17132694
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.

0
 

Author Comment

by:Gemini532
ID: 17133120
>>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...
0
 
LVL 8

Accepted Solution

by:
sagacitysolutions earned 250 total points
ID: 17133577
Ok, Gemini

Dim myFileNames
Dim myFileName

myFileNames = Session("FileNames")

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


*************************this is the code in "proposals.asp"***************
Dim myFileNames
Dim myFileName

myFileNames = Split(Session("fileName"),"|")

for i = 1 to Ubound(myFileNames)
          attachmentsSQL = "INSERT into [Attachments] (FileName, ProposalId) values ('" & myFileNames(i) & "', '" & rs("ProposalId") & "')"
          objConn.Execute attachmentsSQL
Next

0
 
LVL 8

Expert Comment

by:sagacitysolutions
ID: 17133656
Gemini, Let me know if I am m,isunderstanding this
0
 
LVL 25

Expert Comment

by:SStory
ID: 17137984
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
0
 

Author Comment

by:Gemini532
ID: 17140595
Well the problem is the files are saved on THE DMZ in a folder with write permissions
That's not safe, is it?
0
 
LVL 25

Assisted Solution

by:SStory
SStory earned 250 total points
ID: 17141030
Nothing is technically SAFE if you are getting files from users--unless you really trust the users or the documents can't contain binary data.  All else is a risk to some degree or other.

I'm still not sure what the DMZ has to do with your original question of getting file names into an array.  One far out thought, you could name the files uploaded with a prefix related to the user associated with it--like

1_secret.doc
1_pic.jpg

where 1 is the user or whatever.  Then based on just looking at the directory you would know what record it is associated with just by the filename.

If you needed to move those or do something with them I suppose you could have something watching that folder for new files and reacting--but I guess that is more than you'd really want to do.
0
 

Author Comment

by:Gemini532
ID: 17150834
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...
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Report events not being triggered 8 98
Excel object stays open 19 65
Fibonacci challenge 11 82
strDist challenge 35 84
A short article about problems I had with the new location API and permissions in Marshmallow
This is about my first experience with programming Arduino.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

708 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now