我使用WCF RESTful服務來上傳圖片到我的DATABSE 代碼:上傳圖像到數據庫通過使用WCF RESTful服務
[OperationContract]
[WebInvoke(Method = "POST", UriTemplate = "AddDealImage/{id}")]
long AddDealImage(string id, Stream image);
public long AddDealImage(string id, Stream image)
{
//add convert Stram to byte[]
byte[] buffer = UploadFile.StreamToByte(image);
//create image record for database
Img img = ImgService.NewImage(DateTime.Now.ToFileTime().ToString(), "", buffer, "image/png");
ImgService.AddImage(img);
//return image id
return img.ImageId;
}
public static byte[] StreamToByte(Stream stream)
{
byte[] buffer = new byte[16 * 1024];
using (MemoryStream ms = new MemoryStream())
{
int read;
while ((read = stream.Read(buffer, 0, buffer.Length)) > 0)
{
ms.Write(buffer, 0, read);
}
return ms.ToArray();
}
}
問題: 當我通過iPhone的發帖成功上傳我的照片。返回新的圖像ID,我可以看到在數據庫中創建的新記錄。 但是,當我嘗試將二進制數據庫記錄轉換爲圖像流時:出現錯誤: 「找不到適合完成此操作的成像組件。」
它似乎是MemoryStream已損壞。
//photoBytes from database
MemoryStream photoStream = new MemoryStream(photoBytes)
//Error happened here
var photoDecoder = BitmapDecoder.Create(
photoStream,
BitmapCreateOptions.PreservePixelFormat,
BitmapCacheOption.None);
另外,錯誤時圖像通過WCF RESTful服務上傳纔會發生。 如果圖像是通過網絡表單上傳的,它可以很好地工作。
問:
在哪裏我做錯還是錯過了什麼?
我該如何寫一個測試客戶端來測試這個上傳api?
千恩萬謝
有趣的是,你使用的WebRequest和響應,而不是客戶端。這是否適用於流媒體?我不確定它在使用webrequest時仍然在使用軟件包。我需要有人向我解釋這些概念。謝謝。 – Amy 2011-10-06 22:07:39