You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

将Gmail附件保存至Google Drive时出现未知文件类型问题

Fix Google Drive "Unknown File" Issue for Script-Created Files

Hey there, I’ve run into this exact problem before—super frustrating when the file works locally but Drive doesn’t recognize it! The root cause here is almost always missing or incorrect MIME type metadata when creating the file via your script. Google Drive uses the MIME type to know how to handle the file (preview, open, assign the right icon), so even if your content is perfect, wrong metadata will make it show up as "unknown."

Here’s how to fix it:

Step 1: Find the Correct MIME Type for Your File

First, match your file’s actual content to its official MIME type. Some common examples:

  • Plain text: text/plain
  • PDF: application/pdf
  • Microsoft Word (.docx): application/vnd.openxmlformats-officedocument.wordprocessingml.document
  • CSV: text/csv
  • PNG image: image/png

Look up the exact MIME type for your file format if you don’t know it offhand.

Step 2: Update Your Script to Include the MIME Type

How you do this depends on how you’re creating the file. Here are the two most common scenarios:

Scenario 1: Creating Directly with DriveApp

Use the third parameter to specify the MIME type when creating the file:

function createRecognizableFile() {
  const fileName = "MyDocument.pdf";
  const fileContent = /* Your generated file content here */;
  const mimeType = "application/pdf"; // Replace with your file's MIME type
  
  // Create file with correct metadata
  DriveApp.createFile(fileName, fileContent, mimeType);
}

Scenario 2: Using a Blob (for Binary Files)

For binary content like images or PDFs, create a blob with the correct MIME type first:

function createFileFromBlob() {
  const fileName = "MyImage.png";
  const fileContent = /* Your binary content here */;
  const mimeType = "image/png";
  
  const blob = Utilities.newBlob(fileContent, mimeType, fileName);
  DriveApp.createFile(blob);
}

Step 3: Fix Existing "Unknown" Files

If you already have files stuck as unknown, bulk-update their MIME types with this script (replace folder ID and target MIME type):

function fixExistingUnknownFiles() {
  const folderId = "YOUR_FOLDER_ID"; // Replace with your folder's ID
  const targetMimeType = "application/pdf"; // Replace with your correct MIME type
  
  const folder = DriveApp.getFolderById(folderId);
  // "application/octet-stream" is the default for unrecognized files
  const unknownFiles = folder.getFilesByType("application/octet-stream");
  
  while (unknownFiles.hasNext()) {
    const file = unknownFiles.next();
    file.setContentType(targetMimeType);
    console.log(`Fixed file: ${file.getName()}`);
  }
}

Note: Only run this if you’re 100% sure all unknown files in the folder match the target type.

Why This Works

When you don’t specify a MIME type, Drive defaults to application/octet-stream (generic binary data), which it doesn’t recognize as a previewable file type. By setting the correct MIME type, you’re telling Drive exactly what kind of file it is, so it can handle it properly in the interface.

If your previous attempts caused errors or empty files, chances are they used deprecated methods or mismatched MIME types (e.g., setting a PDF MIME type for plain text content). Double-check that your content matches the MIME type you’re using to avoid those issues.

内容的提问来源于stack exchange,提问作者Bioveld IT

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.28 19:22:38