REST Windows Phone照片上传 [英] REST Windows Phone Photo upload

查看:124
本文介绍了REST Windows Phone照片上传的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我正在尝试使用RestSharp将照片上传到Windows Phone 7应用程序中的REST api,以获取我的获取/发布信息.
post参数如下:


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:

照片: 编码为multipart/form-data的照片
photo_album_id : 现有相册的标识符,它可以是事件或组 相册

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

我创建了请求,但每次返回"{\"details\":\"missing photo parameter\",\"problem\":\"The API request is malformed\"}\n

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

我的照片参数如下:

"--------------------------- 8cd9bfbafb3ca00 \ r \ n内容处置:格式数据;名称= \"文件名\"; filename = \"somefile.jpg \" \ r \ n内容类型:image/jpg \ r \ n \ r \ n(此处列出了一些二进制垃圾)\ r \ n ----------- ------------------ 8cd9bfbafb3ca00-"

"---------------------------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--"

我不太确定显示图像的二进制数据是否有问题(当前在我的PhotoTaskCompleted事件中,我将e.ChosenPhoto的内容读入byte []并将其传递给帮助程序方法来创建表单数据),或者如果我只是没有正确地创建表单.

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;
    }

推荐答案

Windows Phone的吊床使这一过程变得非常简单. 您只需使用AddFile方法将文件添加到请求中,然后将照片流传递给该文件即可.

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);

这篇关于REST Windows Phone照片上传的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆