0

我想在 windows 8.1 中通过BackgroundUploader使用Multipart/related Content-Type上传文件

我的代码如下

BackgroundUploader uploader = new BackgroundUploader();
uploader.SetRequestHeader("Content-Type", "multipart/related; boundary=foo_bar_baz");
uploader.Method = "POST";

// Create upload content
List<BackgroundTransferContentPart> parts = new List<BackgroundTransferContentPart>();

// File metadata
var part = new BackgroundTransferContentPart();
part.SetHeader("Content-Type", "text/plain");
part.SetText(file.DisplayName);
parts.Add(part);

// File
// Here file is of type StorageFile
part = new BackgroundTransferContentPart();
part.SetHeader("Content-Type", file.ContentType);
part.SetFile(file);
parts.Add(part);

UploadOperation upload = await uploader.CreateUploadAsync(new Uri("upload_url",UriKind.Absolute), parts);

await upload.StartAsync().AsTask(cts.token);  // cts is CancellationTokenSource

但是,当我运行此代码时,我得到一个异常说

WinRT 信息:'boundary':如果设置了'Content-Type' 标头,则边界不能为空,并且必须与'Content-Type' 标头中设置的边界匹配。

我的代码有什么问题/缺少什么?

4

2 回答 2

1

如果您使用带有四个参数的 CreateUploadAsync,那么以下内容可能会有所帮助:

var uploader = new BackgroundUploader();
uploader.SetRequestHeader("Content-Type", "multipart/form-data; charset=utf-8; boundary=0xKhTmLbOuNdArY-3770FB3C-534D-4E6B-9BE0-392FDA960F7B");

var upload = await uploader.CreateUploadAsync(uri, parts, "form-data", "0xKhTmLbOuNdArY-3770FB3C-534D-4E6B-9BE0-392FDA960F7B");

await upload.StartAsync();

在没有“”(引号)的“SetRequestHeader”中设置边界。稍后设置与 CreateUploadAsync 的最后一个参数相同的边界。

于 2014-11-18T12:52:58.917 回答
0

如果要传递内容部分的列表,则无需设置 Conent-Type 标头。它是自动设置的。

但是,如果您坚持这样做,请尝试:

uploader.SetRequestHeader(
    "Content-Type", 
    "multipart/form-data; boundary=\"foo_bar_baz\"");

更新:

尝试使用CreateUploadAsync带有四个参数的重载,其中第四个参数是边界。见这里:http: //msdn.microsoft.com/en-us/library/ie/hh923975

于 2014-10-11T19:05:13.033 回答