【问题标题】:Empty Mime-type while uploading file in React js application in Windows OS在 Windows 操作系统的 React js 应用程序中上传文件时为空的 Mime 类型
【发布时间】:2020-04-19 13:35:08
【问题描述】:

我在 React 应用程序中使用以下 HTML 代码来上传文件:

<div className="form-group files">
     <label>Upload Your File </label>
     <input type="file" className="form-control" multiple onChange={this.onChangeHandler} />
</div>

此代码在 lynux os 中运行良好,用于上传(doc 和 docx)文件:从 Ubuntu 机器上传 docx 文件的示例输出为:

File {
       ​ lastModified: 1585969775000
    ​    name: "Resume_2.docx"
       ​ size: 122424
       ​ **type: "application/vnd.openxmlformats-officedocument.wordprocessingml.document"**
    ​    ...
    }

但是当我尝试在 Windows 操作系统上运行相同的代码时,代码中似乎存在 BUG,因为我没有得到预期的输出。 这是windows机器上的输出

File {
       ​ lastModified: 1585968960500
    ​    name: "Resume_2.docx"
       ​ size: 122424
       ​ **type: ""**
    ​    ...
    }

type 属性是EMPTY。这与 .doc、.csv、.ppt、.pptx 等文件的输出相同。 虽然此代码适用于 .pdf、.jpg、.png、.txt 文件。

这是完成代码的Link

【问题讨论】:

    标签: javascript html reactjs file-upload bootstrap-4


    【解决方案1】:

    最好是使用表单数据

    var formData = new FormData();
    
    formData.append('profileFileSrc', this.state.filesrc);
    

    然后做一个这样的方法

    handleProfileChange = (event) => {
        this.setState({
            filesrc: event.target.files[0]
        })
    }
    

    渲染下

       <input  type="file"  onChange={this.handleProfileChange} />
    

    【讨论】:

    • 谢谢你,Sagar,我会努力更新你的。如果可能的话,请你告诉我为什么会出现这个问题。为什么在 Lynux 上一切正常,但在 Windows 机器上却不行。
    • 可能是文件系统差异
    猜你喜欢
    • 2021-09-19
    • 2016-02-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-19
    • 1970-01-01
    • 2020-08-09
    • 2020-08-30
    相关资源
    最近更新 更多