javascript - 错误 : Bad content type. 请使用多部分

标签 javascript angularjs file-upload xmlhttprequest google-cloud-storage

我对文件上传完全陌生......我正在使用 angular-file-upload它创建一个 XHR 外部 AngularJS 将文件上传到谷歌云存储。当我尝试上传时,我不断收到以下错误。我该如何解决这个问题?

400 Bad content type. Please use multipart.

这是我的 Controller 设置:

var uploader = $scope.uploader = new FileUploader({
      url: 'https://www.googleapis.com/upload/storage/v1/b/bucketsbuckets/o?uploadType=multipart',
      headers : {
            'Authorization': 'Bearer ya29.lgHmbYk-5FgxRElKafV4qdyWsdMjBFoO97S75p4vB0G0d6fryD5LASpf3JUY8Av9Yzhp9cQP8IQqhA', 
            'Content-Type': 'multipart/form-data'
      },
      autoUpload:true
  });

最佳答案

对于那些使用 fetch 的用户,只需删除 header 上的 content-type 即可。

我在github上发现了这个问题我引用:

Setting the Content-Type header manually means it's missing the boundary parameter. Remove that header and allow fetch to generate the full content type. It will look something like this:

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

Fetch knows which content type header to create based on the FormData object passed in as > the request body content.

^^ 这对我有用。

关于javascript - 错误 : Bad content type. 请使用多部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30917332/

相关文章:

javascript - 是否可以动态构建此功能?

Javascript计算数组中每种类型的数量

angularjs - ng-repeat 的最后一个元素

angularjs - 访问客户端的根文件夹

angularjs - 在 angular.ui 模态中注入(inject) Angular Controller 依赖项的正确方法

c# - 检查上传文件的类型

javascript - X-editable:如何获取每一行的 id 以便我可以更新单个成员?

javascript - 根据变量传递不同的页面

node.js - 如何使用请求模块在 Node 中的post请求中发送文件数据?

html - 样式化文件上传按钮