POST 要求每个请求只上传一个文件

我正在使用 nestjs 并且我想上传到 amazon s3,但是每个请求都失败并且错误是:

POST requires exactly one file upload per request


这是我的代码实现:


const formData = new FormData();

formData.append('policy', clientPayload.policy);

formData.append('key', clientPayload.key);

formData.append('x-amz-signature', clientPayload['x-amz-signature']);

formData.append('x-amz-algorithm', clientPayload['x-amz-algorithm']);

formData.append('x-amz-date', clientPayload['x-amz-date']);

formData.append('x-amz-credential', clientPayload['x-amz-credential']);

formData.append('success_action_status', '201');

formData.append('success_action_redirect', '');

formData.append('file', file.path);


try {

  const data = await this.http

    .post(AppConfig.awsServices.bucketUrl, formData, {

      headers: {

        'content-type':

          'multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW',

      },

    })

    .toPromise();

这是回应:


data: '<?xml version="1.0" encoding="UTF-8"?>\n' +

  '<Error><Code>InvalidArgument</Code><Message>POST requires exactly one file upload per request.</Message><ArgumentName>file</ArgumentName><ArgumentValue>0</ArgumentValue><RequestId>D602243726B03B53</RequestId><HostId>qjLwo8jak8yb3iWOXp4fbqAw391MW7d/3/9r8AzqA20hAvYd1Kgj0PJIAEv+v9sMckixT9WtUoA=</HostId></Error>'

这里还有图片附件:

http://img1.mukewang.com/648a6e940001a81f19171079.jpg

这是我试图从文档中实现的请求:

https://dev.vdocipher.com/api/docs/book/upload/file.html


侃侃尔雅
浏览 470回答 1
1回答

萧十郎

看起来你实际上并没有上传文件,你只是传递了文件的路径。将其更改为:const formData = new FormData();// your .append() calls here// instead of formData.append('file', file.path);form.append('file', fs.createReadStream('path-to-file'));也不要手动指定内容类型标头,httpService 应该处理这个问题:&nbsp;const data = await this.http&nbsp; &nbsp; .post(AppConfig.awsServices.bucketUrl, formData, { headers: { ...formData.getHeaders() })&nbsp; &nbsp; .toPromise();
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript