Windows Phone使用multipart / form-data发送Bitmap

时间:2015-02-26 19:50:42

标签: windows-phone-8 httpwebrequest boundary

我的要求必须如下:

    Content-Type: multipart/form-data; boundary=---BOUNDARY
     -----BOUNDARY
     name="receipt_photo"; filename="image_filename.jpg"
     Content-Type: image/jpeg
    -----BOUNDARY

我正在尝试使用以下代码发送它

    public async static Task addPhotosToReceipt(BitmapImage image, int idReceipt)
    {
        User user = PhoneApplicationService.Current.State["user"] as User;
        if (user.customer.token != null)
        {
            RestConnector rc = new RestConnector();
            byte[] data;
            try
            {
                using (MemoryStream ms = new MemoryStream())
                {
                    WriteableBitmap btmMap = new WriteableBitmap(image);
                    System.Windows.Media.Imaging.Extensions.SaveJpeg(btmMap, ms, image.PixelWidth, image.PixelHeight, 0, 100);
                    data = ms.ToArray();
                }

                string response = await rc.postAsyncData("/api/v1/receipts/" + idReceipt + "/receipt_photos/?token=" + user.customer.token, data);
                if (response.Contains("error"))
                {
                    MessageBox.Show(response);
                }
            }
            catch (Exception ex)
            {
            }
        }
    }

    //in RestConnector class
    public async Task<string> postAsyncData(string adress, byte[] file)
    {

        using (var client = new HttpClient())
        {
            using (var content = new MultipartFormDataContent("---BOUNDARY"))
            {
                content.Add(new StringContent("-----BOUNDARY\nname=\"receipt_photo\"; filename=\"image_filename.jpg\"\nContent-Type: image/jpeg\n-----BOUNDARY"));

                content.Add(new StreamContent(new MemoryStream(file)));
                using (var message = await client.PostAsync(basicUrl + adress, content))
                {
                    var input = await message.Content.ReadAsStringAsync();
                    return input;
                }
            }
        }
    }

作为回应,我应该获取上传照片的网址,但我得到了默认照片的网址。我认为我的请求有一些东西,因为在Android上它工作正常。

也许我选择了错误的方法从我的位图创建字节?

0 个答案:

没有答案