【发布时间】:2010-06-20 11:17:29
【问题描述】:
我想将图像作为字节数组从 php 传递到 .NET 网络服务。 php客户端如下:
<?php
class Image{
public $ImgIn = array();
}
$file = file_get_contents('chathura.jpg');
$ImgIn = str_split($file);
foreach ($ImgIn as $key=>$val) { $ImgIn[$key] = ord($val); }
$client = new SoapClient('http://localhost:64226/Service1.asmx?wsdl');
$result = $client->PutImage(new Image());
echo $result->PutImageResult;
//print_r($ImgIn);
?>
这是 ASP.NET Web 服务中的 Web 方法:
[WebMethod]
public string PutImage(byte[] ImgIn)
{
System.IO.MemoryStream ms =
new System.IO.MemoryStream(ImgIn);
System.Drawing.Bitmap b =
(System.Drawing.Bitmap)Image.FromStream(ms);
b.Save("imageTest", System.Drawing.Imaging.ImageFormat.Jpeg);
return "test";
}
当我运行它时,图像内容被正确读取到 php 客户端中的 ImgIm 数组中。 (在本例中,图像有 16992 个元素。)但是,当数组传递给 Web 服务方法时,它只包含 5 个元素(图像的前 5 个元素)
我能知道数据丢失的原因是什么吗?我该如何避免呢?
谢谢
【问题讨论】:
-
你不应该在 $result = $client->PutImage(new Image()); 行中传递 $ImgIn 而不是 new Image() 吗?
-
您是否尝试过检查通过网络传输的内容(可能使用 Fiddler 或数据包嗅探器?)
-
@Dan:我已将 ImgIn 声明为 Image 类的属性。因此,当我使用 new Image 时,它的所有属性值都被传递。这是传递参数的另一种方式。 @Rowland:我在 web 方法中放置了一个断点并进行了调试。在那里我注意到那里的字节数组只包含 5 个元素。
标签: php asp.net image web-services bytearray