获取MimeType“八位位集流”对于使用API​​创建的文件夹

发布于 2025-01-18 02:18:33 字数 746 浏览 4 评论 0原文

我正在使用Google Drive API在AppDataFolder内部创建一个文件夹,如下所示:

    const fileMetadata = {
      'name': `folderName`,
      'parents': ['appDataFolder']
    };
    const media = {
      mimeType: 'application/vnd.google-apps.folder',
    };
    const folderCreation = await drive.files.create({
      resource: fileMetadata,
      media: media,
      fields: 'id'
    })

但是,当我列出AppDataFolder中的文件时,我将获得一个带有“ Octet Stream”的文件,而不是“ octet stream”而不是“ application/vnd.google-apps”。文件夹“正如我在创作中所说的那样。这是一个问题,因为稍后,当我想在我创建的此文件夹中创建一个文件时,我不能因为MIME类型(我想),我会收到错误:

UnhandledPromiseRejectionWarning: Error: The specified parent is not a folder.

为什么我的文件夹会收到Octet的MimeType流,我该如何解决?

如果您知道解决方案,请提前感谢。

I'm creating a folder inside the appDataFolder with the google drive api as it follows:

    const fileMetadata = {
      'name': `folderName`,
      'parents': ['appDataFolder']
    };
    const media = {
      mimeType: 'application/vnd.google-apps.folder',
    };
    const folderCreation = await drive.files.create({
      resource: fileMetadata,
      media: media,
      fields: 'id'
    })

But when I list the files inside the appDataFolder I get a file with the mimeType of "octet stream" instead of "application/vnd.google-apps.folder" as I stated on the creation. This is a problem because, later when I want to create a file inside this folder I created, I can't because of the mime type (I imagine) , and I get the error:

UnhandledPromiseRejectionWarning: Error: The specified parent is not a folder.

So why is my folder receiving a mimetype of octet stream and how can I solve it?

Thanks in advance if you know the solution.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我爱人 2025-01-25 02:18:33

我需要在 fileMetaData 中包含 mimeType,这与文档所述相反。简单的解决方案,但如果您强烈遵循文档并相信它们是正确的,则很难找到。

I needed to include the mimeType inside the fileMetaData, as opposed as the documentation says. Simple solution but hard to find if you are strongly following the docs trusting they are right.

桃酥萝莉 2025-01-25 02:18:33

您忘记将 mime 类型添加到元数据中。这会告诉驱动器上传时 mime 类型应该是什么。

const fileMetadata = {
      'name': `folderName`,
      'parents': ['appDataFolder'],
      'mimeType' : 'application/vnd.google-apps.folder'
    };

媒体用于上传文件。使用带有文件上传的媒体,您可以告诉谷歌您正在上传的文件的 MIME 类型。然后使用 mime 类型告诉他们上传后的类型应该是什么。

例如,您可以上传 csv 文件并告诉他们将其转换为 Google 表格。

var fileMetadata = {
  'name': 'My Report',
  'mimeType': 'application/vnd.google-apps.spreadsheet'
};
var media = {
  mimeType: 'text/csv',
  body: fs.createReadStream('files/report.csv')
};
drive.files.create({
  resource: fileMetadata,
  media: media,
  fields: 'id'
}, function (err, file) {
  if (err) {
    // Handle error
    console.error(err);
  } else {
    console.log('File Id:', file.id);
  }
});

You forgot to add the mime type to the metadata. This tells drive what the mime type should be when you upload it.

const fileMetadata = {
      'name': `folderName`,
      'parents': ['appDataFolder'],
      'mimeType' : 'application/vnd.google-apps.folder'
    };

Media is used for uploading files. Using media with a file upload you can tell google what the mime type of the file you are uploading. Then use the mime type to tell them what the type should be once it is uploaded.

For example you could upload a csv file and tell them to convert it to a google sheet.

var fileMetadata = {
  'name': 'My Report',
  'mimeType': 'application/vnd.google-apps.spreadsheet'
};
var media = {
  mimeType: 'text/csv',
  body: fs.createReadStream('files/report.csv')
};
drive.files.create({
  resource: fileMetadata,
  media: media,
  fields: 'id'
}, function (err, file) {
  if (err) {
    // Handle error
    console.error(err);
  } else {
    console.log('File Id:', file.id);
  }
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文