错误:内容类型不正确。请使用multipart

时间:2021-12-15 16:30:45

I'm totally new to file uploads....I'm using angular-file-upload which creates an XHR outside of angularjs to upload files to google cloud storage. When I try to upload I keep getting the error below. How can I resolve this?

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

400 Bad content type. Please use multipart.

Here's my controller setup:

这是我的控制器设置:

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
  });

1 个解决方案

#1


1  

The problem is that the endpoint you're using is for multipart uploads, but not FORM-based multipart uploads. If you set your Content-Type to "multipart/related" instead of "multipart/form-data", you should be able to proceed.

问题是您使用的端点是用于分段上传,而不是基于FORM的分段上传。如果您将Content-Type设置为“multipart / related”而不是“multipart / form-data”,那么您应该能够继续。

A multipart upload to that endpoint, "www.googleapis.com/upload/storage/etc?uploadType=multipart", expects a multipart message with exactly two parts, the first part being the metadata of the object, in JSON, and the second part being the data.

向该端点进行分段上传,“www.googleapis.com/upload/storage/etc?uploadType=multipart”,需要一个包含两部分的多部分消息,第一部分是对象的元数据,采用JSON,第二部分是部分是数据。

More on these requirements are here: https://cloud.google.com/storage/docs/json_api/v1/how-tos/upload

有关这些要求的更多信息,请访问:https://cloud.google.com/storage/docs/json_api/v1/how-tos/upload

If, however, you'd like to do an upload in the style of a form submission, that's also possible, but the rules are different. In this case, you'll submit a POST to "storage.googleapis.com/BUCKET/OBJECT" with a variety of appropriate form parameters: https://cloud.google.com/storage/docs/reference-methods#postobject

但是,如果您希望以表单提交的方式进行上传,那也是可能的,但规则是不同的。在这种情况下,您将使用各种适当的表单参数向“storage.googleapis.com/BUCKET/OBJECT”提交POST:https://cloud.google.com/storage/docs/reference-methods#postobject

Now, this all assumes you're trying to do an upload as a multipart request for some reason. That might be necessary if you need to set some specific metadata properties on the object, but if not, you may be making things harder for yourself. A simple PUT to "storage.googleapis.com/BUCKET/OBJECT" will work just fine (although I'm not familiar with angular-file-upload and don't know whether it supports non-form-style uploads).

现在,这一切都假设您出于某种原因尝试上传为多部分请求。如果您需要在对象上设置一些特定的元数据属性,那可能是必要的,但如果不需要,您可能会为自己制造更难的东西。 “storage.googleapis.com/BUCKET/OBJECT”的简单PUT工作正常(虽然我不熟悉angular-file-upload,但不知道它是否支持非表单式上传)。

#1


1  

The problem is that the endpoint you're using is for multipart uploads, but not FORM-based multipart uploads. If you set your Content-Type to "multipart/related" instead of "multipart/form-data", you should be able to proceed.

问题是您使用的端点是用于分段上传,而不是基于FORM的分段上传。如果您将Content-Type设置为“multipart / related”而不是“multipart / form-data”,那么您应该能够继续。

A multipart upload to that endpoint, "www.googleapis.com/upload/storage/etc?uploadType=multipart", expects a multipart message with exactly two parts, the first part being the metadata of the object, in JSON, and the second part being the data.

向该端点进行分段上传,“www.googleapis.com/upload/storage/etc?uploadType=multipart”,需要一个包含两部分的多部分消息,第一部分是对象的元数据,采用JSON,第二部分是部分是数据。

More on these requirements are here: https://cloud.google.com/storage/docs/json_api/v1/how-tos/upload

有关这些要求的更多信息,请访问:https://cloud.google.com/storage/docs/json_api/v1/how-tos/upload

If, however, you'd like to do an upload in the style of a form submission, that's also possible, but the rules are different. In this case, you'll submit a POST to "storage.googleapis.com/BUCKET/OBJECT" with a variety of appropriate form parameters: https://cloud.google.com/storage/docs/reference-methods#postobject

但是,如果您希望以表单提交的方式进行上传,那也是可能的,但规则是不同的。在这种情况下,您将使用各种适当的表单参数向“storage.googleapis.com/BUCKET/OBJECT”提交POST:https://cloud.google.com/storage/docs/reference-methods#postobject

Now, this all assumes you're trying to do an upload as a multipart request for some reason. That might be necessary if you need to set some specific metadata properties on the object, but if not, you may be making things harder for yourself. A simple PUT to "storage.googleapis.com/BUCKET/OBJECT" will work just fine (although I'm not familiar with angular-file-upload and don't know whether it supports non-form-style uploads).

现在,这一切都假设您出于某种原因尝试上传为多部分请求。如果您需要在对象上设置一些特定的元数据属性,那可能是必要的,但如果不需要,您可能会为自己制造更难的东西。 “storage.googleapis.com/BUCKET/OBJECT”的简单PUT工作正常(虽然我不熟悉angular-file-upload,但不知道它是否支持非表单式上传)。