【问题标题】:Multipart/related upload using BackgroundUploader in Windows 8.1在 Windows 8.1 中使用 BackgroundUploader 进行分段/相关上传
【发布时间】:2014-10-11 18:54:59
【问题描述】:

我想在 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' 标头。

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

【问题讨论】:

    标签: windows-runtime windows-8.1 windows-phone-8.1 multipart background-transfer


    【解决方案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 的最后一个参数相同的边界。

    【讨论】:

      【解决方案2】:

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

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

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

      更新:

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

      【讨论】:

      • 使用 multipart/form-data 也会产生同样的错误。由于服务器需要多部分/相关,我不能省略它
      猜你喜欢
      • 1970-01-01
      • 2018-09-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-01
      相关资源
      最近更新 更多