使用FILE对象时,我应该使用哪种MIME类型来处理.MSG文件?

4
我尝试从之前上传的文件中获取MIME类型,但返回值为空。是否有库或代码可以在Angular中获取outlook文件的文件类型。例如:对于图像,它显示为“images/png”。请查看一下。对于.msg文件扩展名的outlook文件,它显示为。

enter image description here

但对于.png文件,它显示的文件类型如下:

enter image description here

在这个网站

他们提到不常见的文件扩展名会返回空字符串 如何解决这个问题???请帮我获取 MIME 类型为 application/vnd.ms-outlook application/octet-stream。


你是在询问 MIME 类型应该是什么,还是如何通过编程来确定它? - Quentin
"我已经尝试过...",请提供一个 [mcve]。 - Quentin
2个回答

3

我遇到了一个相同的问题,即浏览器对Outlook-msg文件没有正确返回MIME类型。 File 的type属性显示为空字符串,而不是 application/vnd.ms-outlook

我使用了以下解决方法(通过检查文件扩展名)来解决这个问题:

在我的文件上传逻辑中,我测试拖放或选择的文件是否属于任何一个或多个接受的 MIME 类型。如果有一个没有给定的 MIME 类型的文件,那么我就检查文件扩展名。

进一步的验证逻辑,例如确定给定文件是否真的是 Outlook-message 类型,可以使用您偏好的编程语言在服务器端完成。

    let enableOutlookMimetypeDetection: boolean = true;

    let acceptRegexString: string = "(image/png)|(application/vnd.ms-outlook)"; // maybe you have to escape the slashes in the regex
    let allowOutlookMsgFiles: boolean = enableOutlookMimetypeDetection && acceptRegexString.indexOf("application/vnd.ms-outlook") >= 0;

    let acceptRegex: RegExp = new RegExp(acceptRegexString);
    for (let i: number = 0; i < files.length; i++) {
        if (allowOutlookMsgFiles) {
            if (files[i].name !== null && files[i].name !== undefined && files[i].name.endsWith(".msg")) {
                console.log("outlook msg-file found");
                continue;
            }
        }

        if (!acceptRegex.test(files[i].type)) {
            return false;
        }
    }

    return true;

1

这里是我处理从Outlook保存为'.msg'格式的'mail'类型文件的方法。您无法直接更改文件类型,因此需要制作一个副本,然后设置MIME为'application/vnd.ms-outlook'。随意添加更多类型检查或'else'情况。

  onHandleFileInput(event: any): void {
    if (event?.target as HTMLInputElement & EventTarget) {
      const f = event?.target?.files[0];

      if (f.type === '' && f.name.endsWith('.msg')) {
        const mailFile = new File([f], f.name, { type: 'application/vnd.ms-outlook' });
        this.uploadFile(mailFile);
      } 
    }
  }


这种情况只发生在.msg文件上吗?还有其他例外情况我们需要考虑吗? - undefined

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接