我试图在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.';
?>
到目前为止,我有:
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();
}
问题是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");
我确信我遗漏了一些非常基本的东西,所以任何指针都会非常感激。
答案 0 :(得分:1)
您正在调用Response.Write(Request.InputStream)
并假设这会将输入流中的所有数据复制到输出流。我认为没有理由相信这种情况。我强烈怀疑它会在输入流上调用ToString()
,然后将其作为文本数据写出来。
我建议你试试这个:
CopyStream(Request.InputStream, Response.OutputStream);
其中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);
}
}