简体   繁体   English

HTTP_RAW_POST_DATA的.NET c#等效项是什么?

[英]What is the .NET c# equivilent of HTTP_RAW_POST_DATA?

I am trying to mimic the following PHP code in C# 我正在尝试在C#中模仿以下PHP代码

<?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: 问题是Request.InputStream不包含从以下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. 我敢肯定我确实缺少一些基本知识,因此任何指针都将不胜感激。

You're calling Response.Write(Request.InputStream) and assuming that that will copy all the data from the input stream to the output stream. 您正在调用Response.Write(Request.InputStream)并假定它将把所有数据从输入流复制到输出流。 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. 我强烈怀疑它会在输入流上调用ToString() ,然后将其写为文本数据。

I suggest you try this: 我建议你试试这个:

CopyStream(Request.InputStream, Response.OutputStream);

where CopyStream is a utility method implemented like this: 其中CopyStream是一种实现如下的实用程序方法:

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);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM