【问题标题】:IE11 converting .xlsx files to .xls and .docx files to .docIE11 将 .xlsx 文件转换为 .xls 和 .docx 文件转换为 .doc
【发布时间】:2021-01-27 04:41:04
【问题描述】:

我有一个 java 应用程序,用户可以在其中上传和下载文件。最近,我们发现每当用户点击 IE11 上的链接下载 .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;
}

【问题讨论】:

  • IE浏览器不会自行将.docx转为.doc。此外,没有从 IE 端解决此问题的设置。您需要调试代码以找到问题的原因。如果可能,请尝试提供任何示例代码来检查问题。尝试提供有关该问题的详细信息。告诉我们,您安装了哪个版本的 MS Office 以及您使用的是哪个操作系统版本?
  • @Deepak-MSFT 添加了下载文件的代码。我正在从数据库中读取文件内容。下载文件时没有错误信息,无法调试。
  • 我想和你确认一下这个问题是否可以在任何机器上使用 IE 11 浏览器产生?还是只能在特定机器上生产?调用函数时是否传递了带有文件名的扩展名?
  • 我相信它发生在所有机器上。许多用户报告了此问题。文件名包含调用函数时的扩展名。
  • 我尝试检查您上面发布的代码并尝试在我这边运行它。我发现很多方法都丢失了,我不确定你为参数传递了什么值。由于这个原因,我无法运行代码。我还注意到您正在使用文件名设置res.setContentType(getContentType(fName));。我不确定你为什么在这里传递文件名。如果有任何简单的方法可以产生问题,请通知我。我会再次尝试检查问题。

标签: java ms-office internet-explorer-11


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-07-29
    • 2012-05-11
    • 2011-06-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-22
    • 2016-07-11
    相关资源
    最近更新 更多