IE11 将 .xlsx 文件转换为 .xls,将 .docx 文件转换为 .doc

IE11 converting .xlsx files to .xls and .docx files to .doc

我有一个 java 应用程序,用户可以在其中上传和下载文件。最近,我们发现每当用户在 IE11 上点击 link 下载 .docx 或 .xlsx 文件时,它会下载一个 .doc 或 .xls 文件。在此过程中,它会警告用户文件格式和扩展名不匹配,用户只有在信任其来源的情况下才能打开该文件。 Microsoft Edge 或其他浏览器上不存在此类问题。 是否可以在 IE11 中完成一些设置,或者是否可以完成一些编码(特定于 IE11),以便它按原样下载 .xlsx 和 .docx 文件并且不会向用户发出烦人的警告消息?

            try {
            byte[] fileContent = getFileContent(id, fName);
            if (fileContent != null) {
                OutputStream out = null;
                try {
                    System.out.println("content type: "+getContentType(fName)); //prints application/vnd.ms-excel
                    res.reset();
                    out = res.getOutputStream();
                    res.setContentType(getContentType(fName));
                    res.setHeader("Content-Disposition", "inline; filename=" + fName + "; size=" + String.valueOf(fileContent.length));
                    res.setContentLength(fileContent.length);
                    out.write(fileContent);
                    setDestination(req, RESPONSE_NO_REDIRECT);
                } catch (Exception ex) {
                    ex.printStackTrace();
                } finally {
                    flushCloseOutputStream(out);
                }
            } else {
                setDestination(req, "/404.jsp");
            }
        } catch (Exception ex) {
           ex.printStackTrace();
        }
        
        
    public byte[] getFileContent(int id, String fileName) {
    byte[] bytes = null;
    Transaction tx = null;
    Session s = null;
    try {
        GenericDAO dao = HibernateDAOFactory.getInstance().getDAO(GenericClassDAO.class, Files.class);
        s = SessionAndTransactionManagementService.createNewSession(dao);
        tx = SessionAndTransactionManagementService.startNewTransaction(s);
        Criteria cr = s.createCriteria(Files.class)
                .add(Restrictions.eq("id", id))
                .add(Restrictions.eq("fileName", fileName))
                .setProjection(Projections.property("fileContent"));
        bytes = (byte[]) cr.uniqueResult();
        SessionAndTransactionManagementService.commitTransaction(s);
    } catch (Exception e) {
        HibernateUtil.rollback(tx);
        
    }finally{
        HibernateUtil.cleanupResources(s);
    }
    return bytes;
}

IE11 将所有 excel 文件的内容类型设置为 'application/vnd.ms-excel'(不知道为什么)。这使得它显示警告并将 xlsx 文件下载为 xls。

当文件名包含 .xlsx 时,我更改了代码以手动将 contentType 设置为 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',这解决了我的问题。