tags:

views:

485

answers:

2

Hi,

I am trying to mimic the following PHP code in C#

<?php

if ( isset ( $GLOBALS["HTTP_RAW_POST_DATA"] )) {

    // get bytearray
    $im = $GLOBALS["HTTP_RAW_POST_DATA"];

    // add headers for download dialog-box
    header('Content-Type: image/jpeg');
    header("Content-Disposition: attachment; filename=".$_GET['name']);
    echo $im;

}  else echo 'An error occured.';

?>

So far I have:

 public ActionResult GetPostedImage(string name)
        {
            var res = Response;
            res.Clear();
            res.Cache.SetCacheability(HttpCacheability.NoCache);
            res.ContentType = "image/jpeg";

            res.AppendHeader("Content-Disposition", "filename=\"" + name + "\"");
            res.Write(Request.InputStream);

            return View();
        }

Problem is that the Request.InputStream does not contain the raw image data posted from the following Flash Actionscript:

var jpgSource:BitmapData = new BitmapData(mc_avatar.width, mc_avatar.height);
jpgSource.draw(mc_avatar);
trace(jpgSource);

var jpgEncoder:JPGEncoder = new JPGEncoder(85);
var jpgStream = jpgEncoder.encode(jpgSource);
var header:URLRequestHeader = new URLRequestHeader("Content-type", "application/octet-stream");
var jpgURLRequest:URLRequest = new URLRequest("/cms3/getpostedimage?name=bloke.jpg");
jpgURLRequest.requestHeaders.push(header);
jpgURLRequest.method = URLRequestMethod.POST;
jpgURLRequest.data = jpgStream;
navigateToURL(jpgURLRequest, "_self");

I am sure I am missing something really basic, so any pointers would be much appreciated.

+1  A: 

You're calling Response.Write(Request.InputStream) and assuming that that will copy all the data from the input stream to the output stream. I see no reason to believe that's the case. I strongly suspect it will call ToString() on the input stream, and then write that out as text data.

I suggest you try this:

CopyStream(Request.InputStream, Response.OutputStream);

where CopyStream is a utility method implemented like this:

public static void CopyStream(Stream input, Stream output)
{
    byte[] buffer = new byte[8192];
    int read;
    while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
    {
        output.Write(buffer, 0, read);
    }
}
Jon Skeet
Hi Jon, you are exactly right! I played around with it and realise that it was calling the ToString(). So I put: var buffer = new byte[Request.InputStream.Length]; Request.InputStream.Read(buffer, 0, buffer.Length); res.BinaryWrite(buffer);Yours of course is better so will use that :-)
Richard
Yes, it's not a good idea to ignore the result of Stream.Read - there's no guarantee you'll get the results in a single call.
Jon Skeet
+1  A: 

I modified it a little to make working for me-

http://dotnetways.blogspot.com/2010/01/saving-image-from-binary-stream.html

Arpit Khandelwal