?
Solved

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

Posted on 2003-02-24
6
Medium Priority
?
561 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
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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

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!

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…
I would like to start this tip/trick by saying Thank You, to all who said that this could not be done, as it forced me to make sure that it could be accomplished. :) To start, I want to make sure everyone understands the importance of utilizing p…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Suggested Courses
Course of the Month12 days, 21 hours left to enroll

578 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