Search code examples
javascriptjavajspresponseoutputstream

How to write javascript code in response alongwith file bytes


I have written code for downloading the file which runs automatically when the page is loaded. I want to perform some javascript functions in the page when page is loaded e.g on window load function.

Following is my code to write file Bytes in response how to write javascript in response too.

  try{
    data = exporter.getData(exportRoot);

    if(data==null)
    {
        throw new Exception("Unable to Generate the Presentation file.");
    }
    response.setContentType("application/octet-stream");
    response.setHeader("Content-Disposition", "attachment;filename=smartArt_presentation.pptx");
    response.setHeader("Cache-Control", "public");

    str.write(data);
    str.flush();
}
catch(Exception e)
{
    String msg = "Error : "+ e.getMessage() ;
    str.write(msg.getBytes());
    str.flush();

}

Solution

  • You can just assemble it as string like this: str.write("javascript:alert('hello');");