简体   繁体   中英

Google App Engine opens excel file in the browser instead of downloading - Vaadin

I am currently trying to create a web application. My task is simple, I am creating a excel file dynamically and create a link for client to download this file. I am using vaadin 6.8.9 and google app engine. When I test the code locally everything works fine and I can download the file but when I deploy the code to the google app engine it tries to open the excel file in the browser. Please help me to fix it. I tried every possible solution but it did not worked.

OR anyone can show me how to listen HttpServletRequestListener in a vaadin class which is not an application class. And set its Content-disposition and write the file.

here is the link for the deployed code, http://vaadingo.appspot.com/

Here is my code,

            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            WritableWorkbook workBook;
            try {
                workBook = Workbook.createWorkbook(outputStream);
                WritableSheet sheet = workBook.createSheet("User List", 0);

                // Generates Headers Cells
                WritableFont headerFont = new WritableFont(WritableFont.TAHOMA, 12, WritableFont.BOLD);
                WritableCellFormat headerCellFormat = new WritableCellFormat(headerFont);
                try {
                    headerCellFormat.setBackground(Colour.PALE_BLUE);
                } catch (WriteException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
                try {
                    sheet.addCell(new jxl.write.Label(1, 1, "LastName", headerCellFormat));
                    sheet.addCell(new jxl.write.Label(2, 1, "FirstName", headerCellFormat));
                    WritableFont dataFont = new WritableFont(WritableFont.TAHOMA, 12);
                    WritableCellFormat dataCellFormat = new WritableCellFormat(dataFont);
                    sheet.addCell(new jxl.write.Label(1, 2, "last", dataCellFormat));
                    sheet.addCell(new jxl.write.Label(2, 2, "first", dataCellFormat));
                    try {
                        workBook.write();
                        workBook.close();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }

                } catch (RowsExceededException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (WriteException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                final ByteArrayInputStream in = new ByteArrayInputStream(outputStream.toByteArray());


                 StreamSource source = new StreamSource() {

                    public InputStream getStream() {
                        // TODO Auto-generated method stub

                        return in;
                    }
                };

                String namefile = "deneme.xls";
                StreamResource resource = new StreamResource(source, namefile, getApplication());

                resource.getStream().setParameter("Content-Disposition", "attachment; filename=\"" + namefile + "\"");
                resource.setMIMEType("application/vnd.ms-excel");
                resource.setCacheTime(-1);
                Link link = new Link();
                link.setCaption("dosya");
                link.setResource(resource);
                link.setTargetName("_blank");
                mainLayout.addComponent(link);

尝试在回复中添加内容处置标头:

Content-Disposition: attachment

Maybe it is not answer but you can set in you browser if some mimes should be opened or downloaded - it also determines what will happen.

Check HTTP response. 1. (Ctrl+Shift+J) Use chrome -> inspect -> network 2. Download file 3. Use chrome -> inspect -> network -> file name -> headers/response (see what headers is set if not set try to solve it or report bug to GAE).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM