REST Windows Phone 照片上传

发布于 2024-10-17 15:58:03 字数 2323 浏览 5 评论 0原文


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

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

,但每次返回"{\"详细信息\":\"缺少照片参数\",\"问题\":\"API 请求格式错误\"}\ n

我的照片参数如下所示:

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

我不太确定这是否是我如何呈现图像的二进制数据的问题(目前在我的 PhotoTaskCompleted 事件中,我读到e.ChosenPhoto 的内容转换为 byte[] 并将其传递给辅助方法以创建表单数据)或者如果我没有正确创建表单。

我只是想尽可能简单地做到这一点,然后一旦我知道它是如何工作的,我就可以重构。

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

俯瞰星空 2024-10-24 15:58:03

适用于 Windows Phone 的 Hammock 让这变得非常简单。
您只需使用 AddFile 方法将文件添加到请求中,并将其传递给照片流。

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

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);
风吹短裙飘 2024-10-24 15:58:03

嗯,你确定你的 PostForm 是正确的吗? content-* 参数应该设置在 POST 的标题中,而不是正文中?

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

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";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文