Link to home
Start Free TrialLog in
Avatar of rossmcm
rossmcmFlag for New Zealand

asked on

Accessing POSTed binary data

I am uploading a binary file to a server running ASP.  The upload occurs from a web page that has a simple form that has a browse/file upload button on it.  The ASP page follows.  After reading the binary data into the string, I search for the uploaded file part of the form data and extract it to a string in an attempt to get at the binary data (not shown).

My intention is to process the uploaded binary file and use the data to generate a response that is served back to the browser.

The problem is that the uploaded file data I extract into the string seems to have the bit 8 of each character stripped off.  Ive tried various charsets with varying (wrong) results.

I know I can use an existing ASP upload component but I don't need to baggage they have and I was trying to code something leaner.

Did the MSB binary data make it?  If I were to save the binary data stream to a file would the MSB's be there?  I don't want to save to a file and read it back.  I'd rather process the data at this point.  How can I access each byte of data in the SafeArray BinaryData? Or perhaps read it byte by byte from the stream?  


<%
var InputStream = Server.CreateObject ("ADODB.Stream") ;
InputStream.Type = 1 ;
InputStream.Open () ;
var BinaryData = Request.BinaryRead (Request.TotalBytes) ;
InputStream.Write (BinaryData) ;

InputStream.Position = 0 ;
InputStream.Type = 2 ; // adTypeText=2
InputStream.Charset = "us-ascii" ;
var StringData = InputStream.ReadText () ;
Response.Write ("StringData = ") ;
Response.Write (StringData) ;
Response.Write ("<br><br>") ;
%>

Open in new window

Avatar of CCongdon
CCongdon
Flag of United States of America image

Does your sending form have multi-part set as its type?
SOLUTION
Avatar of CCongdon
CCongdon
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
Avatar of rossmcm

ASKER

Yes.  If I load the binary data into a string, I can parse the string, find the start boundary of the posted data, the end boundary, then do a substring of the data to get the uploaded file contents.  Trouble is, the MSB is stripped from all the characters.  Actual code follows (with debug statements).

After this code has executed the string variable UploadedFileContents has the correct number of bytes, but no MSBs.

var InputStream = Server.CreateObject ("ADODB.Stream") ;
InputStream.Type = 1 ;
InputStream.Open () ;
var BinaryData = Request.BinaryRead (Request.TotalBytes) ;
InputStream.Write (BinaryData) ;

InputStream.Position = 0 ;
InputStream.Type = 2 ; // adTypeText=2
InputStream.Charset = "us-ascii" ;
var StringData = InputStream.ReadText () ;

//  Get the boundary header

var EndOfStartBoundaryPosition = 0 ;
var EndBoundaryPosition = 0 ;
var BoundaryText = "" ;
var ContentTypePosition = 0 ;
var UploadedFileStartPosition = 0 ;
var UploadedFileLength        = 0 ;
var EndBoundaryPosition = 0 ;
var UploadedFileContents = "" ;

EndOfStartBoundaryPosition = StringData.indexOf ("\r\n", 0) ;
Response.Write ("EndOfStartBoundaryPosition = " + EndOfStartBoundaryPosition + "<br><br>") ;

BoundaryText = StringData.substring (0, EndOfStartBoundaryPosition) ;
Response.Write ("BoundaryText = " + BoundaryText + "<br><br>") ;

ContentTypePosition = StringData.indexOf ("Content-Type:", EndBoundaryPosition) ;
Response.Write ("ContentTypePosition = " + ContentTypePosition + "<br><br>") ;

UploadedFileStartPosition = StringData.indexOf ("\r\n", ContentTypePosition) + 4 ;
Response.Write ("UploadedFileStartPosition = " + UploadedFileStartPosition + "<br><br>") ;

EndBoundaryPosition  = StringData.indexOf (BoundaryText, UploadedFileStartPosition) ;
Response.Write ("EndBoundaryPosition = " + EndBoundaryPosition + "<br><br>") ;

UploadedFileLength   = EndBoundaryPosition - UploadedFileStartPosition - 2 ;
Response.Write ("UploadedFileLength = " + UploadedFileLength + "<br><br>") ;

UploadedFileContents = StringData.substring (UploadedFileStartPosition, UploadedFileStartPosition + UploadedFileLength) ;

Response.Write ("UploadedFileContents = " + UploadedFileContents) ;
Response.Write ("<br><br>") ;

Open in new window

ASKER CERTIFIED 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
Avatar of rossmcm

ASKER

I have submitted my own solution to the problem and request that the question be marked as answered and not deleted as I believe it is still of value.
Avatar of rossmcm

ASKER

I have submitted my own solution to the problem and request that the question be marked as answered and not deleted as I believe it is still of value.