myfaces-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Apache Wiki <wikidi...@apache.org>
Subject [Myfaces Wiki] Update of "Sending Files" by JohnnyGonzalez
Date Tue, 13 Jun 2006 20:55:18 GMT
Dear Wiki user,

You have subscribed to a wiki page or wiki category on "Myfaces Wiki" for change notification.

The following page has been changed by JohnnyGonzalez:
http://wiki.apache.org/myfaces/Sending_Files

------------------------------------------------------------------------------
  = Sending Files (like PDF) to the Browser =
  
  == Introduction ==
- When you need the user to be able to download a file that is stored somewhere in the server.

+ If you need the user to be able to download a file that is stored somewhere in the server
you may face some problems,
+ like the file getting printed into the content of the page, etc. 
- So the idea is to send the file to the Response, then the user will be presented with a

+ So the idea is to send the file to the Response, setting the right headers, then the user
will be presented with a 
  Open/save dialog for him/her to take the required action.
  
  == How to do it ==
@@ -16, +17 @@

  '''//We must get first our context'''
   FacesContext context = FacesContext.getCurrentInstance();
        
- '''//Then we have to get the Response where to put write our file'''
+ '''//Then we have to get the Response where to write our file'''
        HttpServletResponse response = 
           ( HttpServletResponse ) context.getExternalContext().getResponse();
        
- '''//Now we create some variable we will use for writting the file to the response'''
+ '''//Now we create some variables we will use for writting the file to the response'''
        String filePath = null;
        int read = 0;
        byte[] bytes = new byte[1024];
@@ -28, +29 @@

  '''//Be sure to retrieve the absolute path to the file with the required method'''
        filePath = pathToTheFile; 
        
- '''//Now set the content type for our response, be sure to user the correct'''
+ '''//Now set the content type for our response, be sure to use the best suitable content
type depending on your file'''
- '''//this content type is ok for, lets say some kind of text file (like a csv)'''
+ '''//the content type presented here is ok for, lets say, text files and others (like  CSVs,
PDFs)'''
        response.setContentType("application/pdf");
  
  '''//This is another important attribute for the header of the response'''
@@ -39, +40 @@

        response.setHeader("Content-Disposition", "attachment;filename=\"" +
           fileName + "\""); 
  
- '''//Streams to use'''
+ '''//Streams we will use to read, write the file bytes to our response'''
        FileInputStream fis = null;
        OutputStream os = null;
        
- '''//First we load the file in our InputStream      '''
+ '''//First we load the file in our InputStream'''
           fis = new FileInputStream(new File(filePath ));
           os = response.getOutputStream();
  

Mime
View raw message