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

huangapple go评论77阅读模式
英文:

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

问题

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;
}
英文:

I have a java application where users can upload and download files. Recently, we found out that whenever users click on a link on IE11 to download .docx or a .xlsx file, it downloads a .doc or .xls file. In the process, it warns the users that the file format and extension do not match and the users should only open the file if they trust its source. There is no such issue on Microsoft Edge or other browsers.
Is there some setting that can be done in IE11 or can some coding (specific to IE11) be done so that so that it downloads .xlsx and .docx files as they are and doesn't give annoying warning messages to users?

			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;
}

答案1

得分: 0

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

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

英文:

IE11 was setting content type for all excel files as 'application/vnd.ms-excel' (don't know why). That makes it show warnings and download xlsx files as xls.

I changed my code to set contentType manually to 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' when the filename contains .xlsx and this solved my problem.

huangapple
  • 本文由 发表于 2020年10月13日 04:43:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/64325109.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定