axios 如何在JavaScript中从浏览器发送多部分/相关POST请求?

iq0todco  于 2023-08-04  发布在  iOS
关注(0)|答案(1)|浏览(147)

我想构造一个POST,如下面的链接所述
GoogleDriveMultipartUpload
到目前为止我一直在使用axios,但它不支持multipart/related类型。有没有变通的办法或更直接的解决方案。

vvppvyoh

vvppvyoh1#

这在axios中是不可能的,因为它是doesn't allow multipart requests
但是,您可以使用Google云端硬盘节点库,如下所述:

/**
 * Insert new file.
 * @return{obj} file Id
 * */
async function uploadBasic() {
  const fs = require('fs');
  const {GoogleAuth} = require('google-auth-library');
  const {google} = require('googleapis');

  // Get credentials and build service
  // TODO (developer) - Use appropriate auth mechanism for your app
  const auth = new GoogleAuth({
    scopes: 'https://www.googleapis.com/auth/drive',
  });
  const service = google.drive({version: 'v3', auth});
  const requestBody = {
    name: 'photo.jpg',
    fields: 'id',
  };
  const media = {
    mimeType: 'image/jpeg',
    body: fs.createReadStream('files/photo.jpg'),
  };
  try {
    const file = await service.files.create({
      requestBody,
      media: media,
    });
    console.log('File Id:', file.data.id);
    return file.data.id;
  } catch (err) {
    // TODO(developer) - Handle error
    throw err;
  }
}

字符串

相关问题