开发者

upload image to Database by using WCF Restful service

I am using WCF restful service to upload image to my databse Code:

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

Problem: When i upload my photo via iPhone the POST was Successful. New image id is returned, and I can see the new record created in the database. However when I try to convert binary from DB record to Image Stream: I got error: "No imaging component suitable to complete this operation was found."

it seems that the MemoryStream is corrupted.

//photoBytes from database   
MemoryStream photoStream = new MemoryStream(photoBytes)
    //Error happened here
    var photoDecoder = BitmapDecoder.Create(
                    photoStream,
                    BitmapCreate开发者_StackOverflow社区Options.PreservePixelFormat,
                    BitmapCacheOption.None);

Plus, the error only happens when image is uploaded via WCF Restful service. It works perfectly if the image is uploaded via web form.

Question:

  1. Where did i do wrong or missed?

  2. how can i write a test client to test this upload api?

many thanks


the code above actually works. the part I missed is the transferModel you need to set it to "Streamed" in web.config

Code for testing:

static void Main()
        {
            string filePath = @"C:\Users\Dizzy\Desktop\600.png";

            string url = "http://localhost:13228/ApiRestful.svc/AddDealImage/96";

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            request.Accept = "text/xml";
            request.Method = "POST";

            using (Stream fileStream = File.OpenRead(filePath))
            using (Stream requestStream = request.GetRequestStream())
            {
                int bufferSize = 1024;
                byte[] buffer = new byte[bufferSize];
                int byteCount = 0;
                while ((byteCount = fileStream.Read(buffer, 0, bufferSize)) > 0)
                {
                    requestStream.Write(buffer, 0, byteCount);
                }
            }

            string result;

            using (WebResponse response = request.GetResponse())
            using (StreamReader reader = new StreamReader(response.GetResponseStream()))
            {
                result = reader.ReadToEnd();
            }

            Console.WriteLine(result);
        }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜