Google Cloud Functions HTTP无法成功发布内容类型application / octet-stream

时间:2017-10-25 23:46:39

标签: javascript node.js ajax blob google-cloud-functions

我试图编写一个Google Cloud Functions HTTP处理程序,将构建为Blob的音频文件从网页上传到Google Cloud Storage。基于上面链接的文档,我希望将POST请求的正文作为Node.js Buffer处理,我可以使用Content-Type: application/octet-stream来实现此目的。我是从有xhr请求的网页发送请求的:

// where `var blob` is a Blob of type "audio/wav"
// and size of > 0 generated from the
// WebAudioAPI

var fileReader = new FileReader();
fileReader.onloadend = function() {
  $.ajax({
    type: 'POST',
    url: GOOGLE_CLOUD_FUNCTION_URL,
    data: this.result,
    dataType: 'json',
    processData: false,
    contentType: 'application/octet-stream',
    success: function(resp) {
    },
    error: function(err) {
    }
  });
};
fileReader.readAsArrayBuffer(blob);

我的Google Cloud Functions模块如下所示:

exports.upload = function(req, resp) {

  resp.header('Content-Type','application/json');
  resp.header('Access-Control-Allow-Origin', req.get('origin'));
  resp.header('Access-Control-Allow-Headers', 'Content-Type');

  switch (req.method) {
     case 'POST':
       response.status(200).send({
         contentType: request.get('content-type'),
         body: request.body,
         rawBody: request.rawBody
       });
       break;
  }

};

请求标头显示已附加数据。但是,内容类型未设置为`application / octet-stream'。此外,响应返回:

{
  body: {}
}

要传递给Google Cloud Functions HTTP处理程序的ajax正文的数据的JavaScript type是什么?将正文作为Buffer接受?

  • Blob,File,Typed Array,Array Buffer?

0 个答案:

没有答案