1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Upload - Problem with simple and lean binary file upload

Discussion in 'C#' started by rossmcm, Jan 11, 2010.

  1. #1
    I need to implement an ASP interface where a user uploads a binary file and the file is processed and a response returned. I don't need all the bells and whistles of a 3rd party component. A set of sample files is provided which is the smallest example of the problem I could produce.

    If you upload a binary file, not all of the characters make it to the variable StringData., in particular the ones with the MSB set. I figure this is all to do with encoding and character sets, etc. My problem is I just want the raw data. If I look at the stuff posted with Wireshark, all the data is sent verbatim without any munging. I assume that the data in the result of the call to Request.BinaryRead is similarly intact, but I don't know how to access it directly from the variable BinaryData, which I guess is some sort of array. The binary data is written to a stream and then to a string for parsing the request, but this clobbers the posted binary data.

    If you run the two files below it should demonstrate the problem. You will need to provide it with a short file that has some MSB-set characters in it. You can start with a text file with character strings you can recognise and insert some MSB-set characters with a hex editor.

    I'm certain that BinaryData has the binary image of the file correct and intact, as I can see the binary image get posted if I monitor the HTTP with WireShark. All I want is a clean method of getting access to the binary data.

    I could do it by writing BinaryData to a stream, and then saving the stream to a file on the sever, and then reading back the file, but as long as I have the binary data in memory I want to deal with it at the time.

    TIA
    Ross

    -------------------------------------------------------------------------
    File upload1.asp:

    <br><br>
    <form METHOD="POST" ENCTYPE="multipart/form-data" action="upload2.asp" name="form1" id="form1">
    Select a file to upload: <input TYPE="file" NAME="InputFile" value> <input TYPE="submit" NAME="Upload" Value="Upload">
    </form>
    <br><br>

    -------------------------------------------------------------------------
    File upload2.asp:

    <%@ LANGUAGE = JScript %>
    <html>

    <%
    function Hex2 (ByteValue)

    {
    if (ByteValue < 16)
    {
    return ("0" + ByteValue.toString (16)) ;
    }
    else
    {
    return (ByteValue.toString (16)) ;
    }
    }
    %>

    <%
    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 = "UTF-8" ;
    var StringData = InputStream.Readtext () ;
    Response.Write ("StringData = ") ;
    Response.Write (StringData) ;
    Response.Write ("<br><br>") ;
    SEMrush
    Response.Write ("StringData as hex = ") ;
    var ByteData ;
    for (var Index = 0;Index < StringData.length;Index ++)
    {
    ByteData = StringData.charCodeAt (Index) ;
    Response.Write (Hex2 (ByteData) + " ") ;
    }
    Response.Write ("<br><br>") ;
    %>
    </html>
     
    rossmcm, Jan 11, 2010 IP
    SEMrush