开发者

REST Windows Phone Photo upload

I'm trying to upload a photo to a REST api in a Windows Phone 7 application using RestSharp for my Gets/Posts.

The post parameters are as follows:

photo: The photo, encoded as multipart/form-data

photo_album_id: Identifier of an existing photo album, which may be an event or group album

I've created my request, but every time I get back "{\"details\":\"missing photo parameter\",\"problem\":\"The API request is malformed\"}\n

My photo parameter looks like this:

"---------------------------8cd9bfbafb3ca00\r\nContent-Disposition: form-data; name=\"filename\"; filename=\"somefile.jpg\"\r\nContent-Type: image/jpg\r\n\r\n(some binary junk listed here)\r\n-----------------------------8cd9bfbafb3ca00--"

I'm not quite sure if it's a problem with how I'm presenting the binary data for the image (currently in my PhotoTaskCompleted event, I read the contents of e.ChosenPhoto into a byte[] and pass that to a helper method to create the form data) or if I just don't create the form correctly.

I'm just trying to do this a simple as possible, then I can refactor once I know how it all works.

 void ImageObtained(object sender, PhotoResult e)
    {

        var photo = ReadToEnd(e.ChosenPhoto);
        var form = PostForm(photo);
        var request = new RequestWrapper("photo", Method.POST);
        request.AddParameter("photo_album_id", _album.album_id);
        request.AddParameter("photo", form);

        request.Client.ExecuteAsync<object>(request, (response) =>
          {
               var s = response.Data;
          });
    }

    private string CreateBoundary()
    {
        return "---------------------------" + DateTime.Now.Ticks.ToString("x");

    }

    private string PostForm(byte[] data)
    {
        string 开发者_如何转开发boundary = CreateBoundary();
        StringBuilder post = new StringBuilder();
        post.Append(boundary);
        post.Append("\r\n");
        post.Append("Content-Disposition: form-data; name=\"filename\"; filename=\"somefile.jpg\"");
        post.Append("\r\n");
        post.Append("Content-Type: image/jpg");
        post.Append("\r\n\r\n");
        post.Append(ConvertBytesToString(data));
        post.Append("\r\n");
        post.Append("--");
        post.Append(boundary);
        post.Append("--");

        return post.ToString();
    }

    public static string ConvertBytesToString(byte[] bytes)
    {
        string output = String.Empty;
        MemoryStream stream = new MemoryStream(bytes);
        stream.Position = 0;
        using (StreamReader reader = new StreamReader(stream))
        {
            output = reader.ReadToEnd();
        }

        return output;
    }


Hammock for Windows Phone makes this real simple. You just add the file to the request using the AddFile method and pass it the photo stream.

        var request = new RestRequest("photo", WebMethod.Post);
        request.AddParameter("photo_album_id", _album.album_id);
        request.AddFile("photo", filename, e.ChosenPhoto);


Hum are you sure that your PostForm is correct ? The content-* params should be set in the headers of your POST and not in the body ?

var request = (HttpWebRequest)WebRequest.Create(url);
request.Headers.Add(HttpRequestHeader.Authorization,"blabla");
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜