lundi 15 octobre 2012

Opening streamed file with flex, without saving it


When we send streamed files from the backend to the flex frontend, we have at least two ways to 'read' it.

The first common way is to save it using a FilReference.But this way need a confirmation flex popup, and sometimes the flex application is too goodlooking that you're disgusted when the archaic 'Save As' window is displayed!! Espacially when we don't really need to save the file and only want to have a fast look into the document before may be saving it.

It's why there are a second way to handle this, at least for PDF and XLS documents. (and may be for other file types but you have to google it). The way is to open the streamed document into an other tab using a java servlet, and let the web browser decide wich software would open it.

Here is a code sample for PDF :
public static function exportPdfFromStream(stream:ByteArray,
                                           fileName:String):void {
    var header:URLRequestHeader =
        new URLRequestHeader ("Content-type","application/octet-stream");
    var myRequest:URLRequest =
        new URLRequest (urlApplication+ 'CreatePDF' +
                       '?name='+fileName+'&method=attachment');
    myRequest.requestHeaders.push (header);
    myRequest.method = URLRequestMethod.POST;
    myRequest.data = stream;
    navigateToURL(myRequest,"_blank");
}

For Csv you just need to change the header content-type :
var header:URLRequestHeader =
    new URLRequestHeader("Content-type", "application/vnd.ms-excel");

Here is the way to declare the java servlet :
<servlet>
      <servlet-name>createPDF</servlet-name>
      <servlet-class>
          com.manpower.socle.tools.CreatePDFServlet
      </servlet-class>
      <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
      <servlet-name>createPDF</servlet-name>
      <url-pattern>/client/createPDF</url-pattern>
</servlet-mapping>



And this is the servlet :
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;

public class CreatePDFServlet extends HttpServlet {

      public void doPost(HttpServletRequest req,    
                         HttpServletResponse resp)
             throws ServletException, IOException {
            doGet(req, resp);
      }

      public void doGet(HttpServletRequest req,
                        HttpServletResponse resp)
             throws ServletException, IOException
      {
            int i = 0;
            int k = 0;
            int maxLength = req.getContentLength();
            byte[] bytes = new byte[maxLength];
            String method = req.getParameter("method");
            String name = req.getParameter("name");
            ServletInputStream si = req.getInputStream();
            while (true)
            {
                  k = si.read(bytes,i,maxLength);
                  i += k;
                  if (k <= 0)
                        break;
            }

            if (bytes != null)
            {
                  ServletOutputStream stream =
                        resp.getOutputStream();
                  resp.setContentType("application/pdf");
                  resp.setContentLength(bytes.length);
                  resp.setHeader("Content-Disposition",method +
                                 ";filename=" + name);
                  stream.write(bytes);
                  stream.flush();
                  stream.close();
            }
            else
            {
                  resp.setContentType("text");
                  resp.getWriter().write("bytes is null");
            }
      }
}


Bon courage my friends!

Aucun commentaire:

Enregistrer un commentaire