?
Solved

Can't save a JPEG to disk using ADODB.Stream

Posted on 2003-02-24
6
Medium Priority
?
538 Views
Last Modified: 2012-05-04
With the following code I am able to stream a jpeg to the browser fine but I can't seem to get the image to save to disk.. for some reason my code is corrupting the image.

Another odd problem is trying response.write before and after Response.BinaryWrite when sending directly to the browser. If I response.write plain text my image does not show up, or vice-versa if I response.write after the Response.BinaryWrite.

Any help with this would be greatly appreciated.

Thanks in advance



strURL="http://webaddress.whatever"
strFilePath = "c:\path_to_dir\search\imaging\photos\testing123.jpg"
set objXMLHTTP1 = Server.CreateObject("Microsoft.XMLHTTP")
objXMLHTTP1.Open "POST", strURL,false
objXMLHTTP1.Send

Response.Buffer = TRUE
Response.Expires = 0
Response.Clear
Response.ContentType = "image/jpeg"

'***** THIS WORKS
Response.BinaryWrite(objXMLHTTP1.responseBody)

const adTypeBinary = 1
const adModeWrite = 2
const adModeReadWrite = 3
const adCreateOverwrite = &H4000000

Set objStream = Server.CreateObject("ADODB.Stream")
objStream.Type = adTypeBinary
objStream.Open  

'***** THIS DOES NOT
objStream.SaveToFile strFilePath
objStream.close
Set objStream = Nothing
Set objXMLHTTP1 =nothing






0
Comment
Question by:spamuel
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 7

Expert Comment

by:pegasys
ID: 8007244
Maybe this could help, it's an upload script, without the use of upload components:




<%@ Language=VBScript %>
<%Option Explicit%>
<!-- #include file="inc/upload.asp" -->
<%

'NOTE - YOU MUST HAVE VBSCRIPT v5.0 INSTALLED ON YOUR WEB SERVER
'        FOR THIS LIBRARY TO FUNCTION CORRECTLY. YOU CAN OBTAIN IT
'        FREE FROM MICROSOFT WHEN YOU INSTALL INTERNET EXPLORER 5.0
'        OR LATER.


' Create the FileUploader
Dim Uploader, File
Set Uploader = New FileUploader

' This starts the upload process
Uploader.Upload()

'******************************************
' Use [FileUploader object].Form to access
' additional form variables submitted with
' the file upload(s). (used below)
'******************************************
Response.Write "<b>Thank you for your upload " & Uploader.Form("fullname") & "</b><br>"

' Check if any files were uploaded
If Uploader.Files.Count = 0 Then
     Response.Write "File(s) not uploaded."
Else
     ' Loop through the uploaded files
     For Each File In Uploader.Files.Items
         
          ' Check where the user wants to save the file
          If Uploader.Form("saveto") = "disk" Then
     
               ' Save the file
               File.SaveToDisk "C:\inetpub\coolprofile\userimages"
     
          ElseIf Uploader.Form("saveto") = "database" Then
               
               ' Open the table you are saving the file to
               Set RS = Server.CreateObject("ADODB.Recordset")
               RS.Open "MyUploadTable", "CONNECT STRING OR ADO.Connection", 2, 2
               RS.AddNew ' create a new record
               
               RS("filename")    = File.FileName
               RS("filesize")       = File.FileSize
               RS("contenttype") = File.ContentType
         
               ' Save the file to the database
               File.SaveToDatabase RS("filedata")
               
               ' Commit the changes and close
               RS.Update
               RS.Close
          End If
         
          ' Output the file details to the browser
          Response.Write "File Uploaded: " & File.FileName & "<br>"
          Response.Write "Size: " & File.FileSize & " bytes<br>"
          Response.Write "Type: " & File.ContentType & "<br><br>"
     Next
End If

%>
0
 
LVL 7

Expert Comment

by:pegasys
ID: 8007249
Sorry:

C:\inetpub\coolprofile\userimages <-- is the "save to path"

Regards

pgx
0
 

Author Comment

by:spamuel
ID: 8007378
Thanks for the reply pegasys. I actually found a solution right after posting my question. It turns out I should have been using MSXML2 instead... the function below retrieves binary data and saves it to disk.


Function getImage()

 On Error Resume Next
 strImageURL = "webADDRESS.com/blah/blah"

 Set xml = Server.CreateObject("MSXML2.ServerXMLHTTP")
 Set oStream = Server.CreateObject("ADODB.Stream")

 xml.Open "GET", strImageURL, False
 xml.Send

 oStream.Type = 1 'adTypeBinary
 oStream.Open
 oStream.Write xml.responseBody
 oStream.SaveToFile strFileName, 2 'adSaveCreateOverWrite

 oStream.Close

 Set xml = Nothing
 Set oStream = Nothing

 If Err.Number <> 0 Then
  getImageOrder = False
 Else
  getImageOrder = True
 End If

End Function
0
Independent Software Vendors: 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!

 

Expert Comment

by:CleanupPing
ID: 9576923
spamuel:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0
 
LVL 58

Expert Comment

by:Gary
ID: 9754010
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

PAQ'd and pts refunded

Please leave any comments here within the next seven days.
 
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
 
GaryC123
EE Cleanup Volunteer
0
 
LVL 1

Accepted Solution

by:
Computer101 earned 0 total points
ID: 9803934
PAQed, with points refunded (250)

Computer101
E-E Admin
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have helped a lot of people on EE with their coding sources and have enjoyed near about every minute of it. Sometimes it can get a little tedious but it is always a challenge and the one thing that I always say is:   The Exchange of informatio…
This demonstration started out as a follow up to some recently posted questions on the subject of logging in: http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28634665.html and http://www.experts-exchange.com/Programming/…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

777 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