C#MultiPartParseError:多部分中的无效边界(无):

时间:2014-06-14 04:50:00

标签: c# forms windows-runtime django-rest-framework windows-phone-8.1

MultiPartParseError: Invalid boundary in multipart: None

我可以通过浏览器上传图像(和描述)没问题。

当我尝试通过HttpMultipartFormDataContent Windows Phone 8.1运行时)上传图像和说明时,我收到了边界错误(即使我指定了边界!!)。

这里可能出现什么问题?我的内容类型是否正确?我错过了标题吗?我无法识别边界。

HttpClient httpClient = new HttpClient();

try
{

    string boundary = "---------------------------857372834998310";
    HttpMultipartFormDataContent form = new HttpMultipartFormDataContent(boundary);

    form.Headers.ContentType = HttpMediaTypeHeaderValue.Parse("multipart/form-data");

    // String
    HttpStringContent stringContent = new HttpStringContent(("description=" + formData), Windows.Storage.Streams.UnicodeEncoding.Utf8);
    stringContent.Headers.ContentType = HttpMediaTypeHeaderValue.Parse("application/x-www-form-urlencoded");
    form.Add(stringContent, "data");

    // Image
    var fileStream = await formFile.OpenReadAsync();
    HttpStreamContent streamContent = new HttpStreamContent(fileStream);
    streamContent.Headers.ContentType = HttpMediaTypeHeaderValue.Parse(fileStream.ContentType);
    form.Add(streamContent);

    // Let a rip
    HttpResponseMessage result = await httpClient.PostAsync(imageUploadUri, form);

    // ... boop ... beep ...
    // 010010100101001001001
    // ... beep ... boop ...

    // Receive the response
    String jsonResponse = await result.Content.ReadAsStringAsync();
    await new MessageDialog(jsonResponse).ShowAsync();
}

我在我的API中使用Django和Django-Rest-Framework。

1 个答案:

答案 0 :(得分:0)

我在内容类型中包含了边界:

form.Headers.ContentType = HttpMediaTypeHeaderValue.Parse("multipart/form-data; boundary=" + boundary);

所以我的内容类型如下:

Content-Type: multipart/form-data; boundary=---------------------------857372834998310