Hi, I have a file( word or excel) in my web folder. In my application I have a hyperlink. On click of which I have to open the excel in such a way that it should allow to open or save it at my desired location through the open/save dialog box. Can anyone help me out???
Hi, I am using the following code :: String filePath = "C:\\Abc.xls"; File f1 = new File(filePath); response.setContentType("application/vnd.ms-excel"); response.setHeader("Content-Disposition","attachment; filename=\"Abc.xls\""); InputStream fis = new FileInputStream(f1 ); byte[] bytearray = new byte[(int) f1.length()]; ServletOutputStream outs = response.getOutputStream(); fis.read(bytearray); outs.write(bytearray); outs.flush(); outs.close(); fis.close();
yet it is not working. Do you have any suggestion whjere I am going wrong. If possible can you provide some pseudo code which will help me understand the functionality better..
Are you sure you have your file in the root directory on "C:\\"?
Sekhar Kapoor
Greenhorn
Joined: Apr 24, 2007
Posts: 15
posted
0
The whole code I have given is within a try block and have catch(IOException ioe){ e.printStackTrace(); } in the catch part. ALso the file is in C rive.
Ulf Dittmer
Marshal
Joined: Mar 22, 2005
Posts: 35439
9
posted
0
yet it is not working.
What does this mean? What do you think should be happening, and where does the observed behavior deviate from that?
Sekhar Kapoor
Greenhorn
Joined: Apr 24, 2007
Posts: 15
posted
0
The page is refreshing but still neither the download box nor the excel is opening.I had given system.out so I know that the code is getting executed. yet what I need to achieve is not happening.
Ulf Dittmer
Marshal
Joined: Mar 22, 2005
Posts: 35439
9
posted
0
So what does the browser receive, then?
Sekhar Kapoor
Greenhorn
Joined: Apr 24, 2007
Posts: 15
posted
0
Today I did some modifications to the code. I am now forwarding to a jsp where I have this code:
try { String filename = "C:\\Abc.xls"; // set the http content type to "APPLICATION/OCTET-STREAM response.setContentType("APPLICATION/MSEXCEL"); // initialize the http content-disposition header to // indicate a file attachment with the default filename // "myFile.txt" String disHeader = "Attachment;Filename=\"Abc.xls\""; response.setHeader("Content-Disposition", disHeader); // transfer the file byte-by-byte to the response object File fileToDownload = new File(filename); FileInputStream fileInputStream = new FileInputStream(fileToDownload); int i; /*while ((i=fileInputStream.read())!=-1) { out.write(i); } */ out.write(fileInputStream.read()); fileInputStream.close(); out.close(); } catch(Exception e) // file IO errors { e.printStackTrace(); }
This is opening the the dialog box. But when I am rying to open or save the excel it is opening in one cell with some encode format........ Why is this happening and what is the solution..
Sekhar Kapoor
Greenhorn
Joined: Apr 24, 2007
Posts: 15
posted
0
At last download is working for the excel. But theres one new error coming up. When I try to open the file. It says there is an error Excel repairs the error and then shows the data as a repaired file. The error says, " Excel tried to recover your formulas and values, but some data may have been lost or corrupted." However the excel is opening.
Ulf Dittmer
Marshal
Joined: Mar 22, 2005
Posts: 35439
9
posted
0
It's not a good idea to stream binary content from a JSP, and practically impossible to get right. I am guessing that the problem would go away if you used a servlet instead.