• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 363
  • Last Modified:

Getting ASP Upload contents into Stream object

Im trying to get the File object that is represrented in my ASP Uplad class, into a Stream object. I need a Stream object so I can write to mySQL.

The code I have is here:

                Set conn = Server.CreateObject("ADODB.Connection")
            conn.ConnectionString = strConnect & " OPTION=" & 1 + 2 + 8 + 32 + 2048 + 16384
            conn.CursorLocation = adUseClient
            conn.Open
            
            Set RS = Server.CreateObject("ADODB.Recordset")
             Set mystream = Server.CreateObject("ADODB.Stream")
             mystream.Type = adTypeBinary
            mystream.Open
            
            rs.Open "Select * from Gourmet_Images WHERE ImageID = "& theimageid, conn, adOpenStatic, adLockOptimistic
            mystream.LoadFromFile FileData
            rs("imageblob") = mystream.Read
               rs("imagepath") = File.FileName
            rs("imagesize") = File.FileSize
               rs("contenttype") = File.ContentType
            rs.Update
      
            mystream.Close
            rs.Close
            rs = nothing
            mystream = nothing

The problem is wtith the line 'mystream.LoadFromFile FileData' - I know this line is wrong, but what should I have?

Thanks for your help.
0
colr__
Asked:
colr__
  • 13
  • 10
1 Solution
 
pratap_rCommented:
hey colr

are you looking for code to upload the file to the server?? because the mystream.LoadFromFile is fine as long as FileData points to the full file path...

eg,
mystream.LoadFromFile "c:\tmp\uploads.bmp"

are you looking at something which will upload the file and write to uploads.bmp?

Have Fun!
Pratap
0
 
pratap_rCommented:
and you might want to look at this

http://swforum.sun.com/jive/thread.jspa?threadID=46426&messageID=137068

its a similar problem for you i guess

Have Fun!
Pratap
0
 
colr__Author Commented:
The code I need is for the upload of a file to the server, using ASPUpload, and for that file to be stored in a mySQL database. Bacause its mySQL, I need to write the data as a Stream object.

I have the uploader working ok, but I need to get the data in the uploaded File object into a Stream object. Once I have  the uploaded data as a Stream object, I can directly go ahead and write the stream to the mySQL database.

I do not have access to the server to allow me to save the uploaded file to disk and then open into a stream. I dont know the paths either, so it cant be done this way.

IOs there not a way to directly convert from a File object into a Stream object? That would solve my problem.

colr__
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
pratap_rCommented:
hmm if i am not wrong there is a UploadedFile object in ASPUpload which exposes a Binary property which you can directly use it as a blob value or load it into the stream using stream.write and then use the object indirectly to save it as a blob.

do you have the specs of ASPUpload? it does expose a stream by itself i guess.

Have Fun!
Pratap
0
 
colr__Author Commented:
Im afraid my ASP isnt that hot - Ive got the object that represents the binary chunk of data, now if I can convert this into a bte array I can write this to the Stream, and then to the mySQL database.

I did try using the built-in writeToDatabase method, which uses Addchunk to write to a recordset. This method wont work with the version of mySQL Im using, and so I need to write from a Stream object. I am in the process of migrating a SQL databse to mySQL, and right now am trying to get the site that sits on top of the database configured. Its not going to be practical to change the ASP Upload script Im using, so Im stuck with trying to work this out.

If I can get a binary chunk (I only kow the name of this object, not the type) converted into a byte array, I can write this to the Stream object and then the mySQL database.

Any ideas? - thanks for your help so far.
0
 
pratap_rCommented:
binary chunk is usually represented by byte.. can you post the function footprint of the method that exposes the binary data?
0
 
colr__Author Commented:
If you look at the first posting I made, you'll notice I refer to a 'File' object to get the uploaded files details. I think this refers to the following sub class of the ASP Uploader:


Class UploadedFile
      Public ContentType
      Public FileName
      Public FileData
      
      Public Property Get FileSize()
            FileSize = LenB(FileData)
      End Property

      Public Sub SaveToDisk(sPath)
            Dim oFS, oFile
            Dim nIndex
      
            If sPath = "" Or FileName = "" Then Exit Sub
            If Mid(sPath, Len(sPath)) <> "\" Then sPath = sPath & "\"
      
            Set oFS = Server.CreateObject("Scripting.FileSystemObject")
            If Not oFS.FolderExists(sPath) Then Exit Sub
            
            Set oFile = oFS.CreateTextFile(sPath & FileName, True)
            
            For nIndex = 1 to LenB(FileData)
                oFile.Write Chr(AscB(MidB(FileData,nIndex,1)))
            Next

            oFile.Close
      End Sub
      
      Public Sub SaveToDatabase(ByRef oField)
            If LenB(FileData) = 0 Then Exit Sub
            
            If IsObject(oField) Then
                  oField.AppendChunk FileData
            End If
      End Sub

End Class

From this I think that 'FileData' is the chunk of data, since this is what us used in the subroutine SaveToDatabase and SaveToDisk. If I can get this info into aStream object, I think I will have cracked it. You can write data into a Stream in binary array frmat, if that helps.

colr__
0
 
colr__Author Commented:
If i pass the Stream object into the SaveToDatabase subroutine above, then I need an alternative to the AppendChunk function that will work on the Stream.

So close, yet so far away . . .
0
 
pratap_rCommented:
hmm... that should do it.. File.FileData that exposes a byte stream. you can use that.

mystream.LoadFromFile File.FileData
0
 
colr__Author Commented:
I think I tried that a while ago, but Imgetting a different error code this time!!

My code reads:

            mystream.Type = adTypeBinary
            mystream.open
            mystream.LoadFromFile File.FileData

The LoadFromFile line is throwing the following error:

            ADODB.Stream error '800a0bba'

            File could not be opened.

            /gourmetsecure/admin/producteditp.asp, line 50

Dont quite understand this as the File object is public in the class above - any ideas what Im doing wrong?

colr__
0
 
pratap_rCommented:
no thats now what i meant.. sorry i guess i pasted the wrong code in my previous post..

File.FileData is the byte stream you are looking for, it is already loaded into memory

All you have to do is assign it to the blob and store it, you dont have to use ADODB.Stream

rs("imageblob") = File.FileData

Sorry for the confusion

Have Fun!
Pratap
0
 
pratap_rCommented:
or you can use that SaveToDatabase function and pass the rs("imageblob") as parameter

File.SaveToDatabase(rs.Fields("imageblob"))
0
 
colr__Author Commented:
I now have the following code:

            Set conn = Server.CreateObject("ADODB.Connection")
            conn.ConnectionString = strConnect
            conn.CursorLocation = adUseClient
            conn.Open
            
            Set RS = Server.CreateObject("ADODB.Recordset")
            
            rs.Open "Select * from Gourmet_Images WHERE ImageID = "& theimageid, conn, adOpenStatic, adLockOptimistic
                        
               rs("imagepath") = File.FileName
            rs("imagesize") = File.FileSize
               rs("contenttype") = File.ContentType
            rs("imageblob") = File.FileData
            
            rs.Update
            rs.Close
            rs = nothing
            mystream = nothing

but Im now getting this error:

            Microsoft Cursor Engine error '80040e21'

            Multiple-step operation generated errors. Check each status value.

            /gourmetsecure/admin/producteditp.asp, line 49

line 49 refers to the rs("imageblob") = File.FileData part. I had already tried the SaveToDatabase thign originally, but that didntwork and thats when I came across an artical that said something about using a Stream object because it is i mySQL database.

colr__
0
 
pratap_rCommented:
can you post the schema of the table or atleast the field type of imageblob ??

i am trying to reproduce the problem on my machine,
0
 
pratap_rCommented:
i checked it with blob data type, seems to work fine for me? are you using any other datatype?

Have Fun!
Pratap
0
 
colr__Author Commented:
Sorry for not getting back sooner.

I have posted my two source code files here for you to view:

      <a href=http://colin-reid.co.uk/experts/producteditp.asp.htm>http://colin-reid.co.uk/experts/producteditp.asp.htm</a>
      <a href=http://colin-reid.co.uk/experts/upload.asp.htm>http://colin-reid.co.uk/experts/upload.asp.htm</a>

The error Im getting is as follows:

     Microsoft Cursor Engine error '80040e21'

     Multiple-step operation generated errors. Check each status value.

     /gourmetsecure/admin/producteditp.asp, line 48

Ive marked this line as bold within the producteditp.asp page.

Its only really this first part im struggling with (everything below the continuous >>>'s can be ignored for this problem). The 'imageblob' field is a blob data type, orignally I had it as longblob, but then changed it back to blob - this didnt have any efect (as expected).

When you tried it yourself, was it a mySQL database you used?

Thansk for the help . . . if you need a java question answered, give me a shout!!!
0
 
pratap_rCommented:
i checked with your code, SaveToDatabase function works fine for me. the byte array assignment fails though. Can you check if SaveToDatabase works for you..?

heres the code I used

            rs.AddNew
            rs("id") = 10
            'File.SaveToDatabase rs("data") 'this works fine
            Response.Write rs("data").type
            rs("data").AppendChunk File.FileData
            rs.update
            rs.close


id was an integer field and data was a blob

Let me know if you face any problems with this.
0
 
colr__Author Commented:
Many apoligies for the delay in replying.

I got the above code to run error freee, using the SaveToDatabase version, although I'm not sure if it actually worked or not.

I ran the following query in DB Manager to see if the image did in fact upload (indicated by its size), but got an empty result:

SELECT BIT_LENGTH(ImageBlob)
FROM Gourmet_Images WHERE Imageid="229".

Now, although the upload section of code didnt throw any errors, Im not sure if it actually worked cause my code to retrieve the image and make it display onscreen, and the above code, dont seem to pick up anything.

How did you check that the blob had in fact been uploaded? Can I see your code for viewing the image?

colr__
0
 
pratap_rCommented:
i fired the query directly, did not use any code to display the image though. i compared the hex dump of the image and the data in the table. they matched. did you add any on error resume next ?? if thats the case then your errors wont appear but your code wont work either..
0
 
colr__Author Commented:
How do I do a hex dump of a mySQL DB? m using DB Manageer.

Almost there . . .

colr__
0
 
pratap_rCommented:
well i used the mysql command line utility.. nothing special.. :-)
0
 
colr__Author Commented:
I didnt quite get my answer working, so not sure what to do with points.

pratap_r did help me tremendously, but never solved the problem. Award half the points?

colr.
0
 
VenabiliCommented:
Or award all with Grade B?:)

If you want half of your points back, post a request for this in CS.
0
 
pratap_rCommented:
I wasn't aware that the problem was not solved. It works fine for me. I used the same code and database setup.If the author can describe the problem may be I can help further. I was under the impression that the problem was solved.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 13
  • 10
Tackle projects and never again get stuck behind a technical roadblock.
Join Now