Content-Disposition Header May Prevent SWF Files From Playing


A Little Background Information for Context

I’m working with Xcelsius (”XC”) and Flex and I had to create a custom SWFLoader component to fix some issues with the out of the box (”OOTB”) SlideShow (”SS”) component in XC…without getting into too much detail, developers are currently using the SS component to load child SWFs into their parent SWFs (main XC Flex application) in order to make the application smaller; however, there’s a bug in the SS component that kills any previous and new LCDS connections created in either the parent or child SWFs — this is bad — so again, I decided to create my own SWFLoader component using the native, AS3 SWFloader class.

So I rolled my own custom SWFLoader component and voila, the LCDS connections and my loader worked as expected in my small PoC. I then ported my work over to our real application, and blam…no loading…nothing…nada…crap.

Naturally I had to locate the differences in my small PoC app and our real app and the one thing that struck me was that the real app was using an OOTB servlet by Business Objects to serve up the SWFs, whereas mine was just loading them by direct URLs. I then proceeded to create my seemingly simple test servlet to do the same. Here’s a simplified version of my Servlet without all the try/catches/error handling in order to be concise and just give you the idea:

SendSWF.java

public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
{
	ServletContext servletContext = getServletContext();
	String fileName = (String) request.getParameter("file");
	String mimetype = "application/x-shockwave-flash";
	InputStream inputStream =
        servletContext.getResourceAsStream("/XcelsiusFlex/" + fileName);

	// set response headers
   response.setContentType(mimetype);
   response.addHeader("Content-Type", mimetype);

	// KILLER - You bruised my head...
   //response.addHeader("Content-Disposition",
   "attachment; filename=" + (String) request.getParameter("file"));

	int read = 0;
	byte[] bytes = new byte[1024];

	OutputStream outputStream = response.getOutputStream();
	while((read = inputStream.read(bytes)) != -1)
	{
		outputStream.write(bytes, 0, read);
	}

	outputStream.flush();
	outputStream.close();
	inputStream.close();
}

Solution

Simple enough, right? Send the binary SWF back to my Flex client and make sure it has the proper header mimetype of:

application/x-shockwave-flash

But it didn’t work…I spent some time banging my head against my MacBook Pro and my partners until one them shot me this link. As you can see in my comments in the code, the line that killed me was adding the “Content-Type” of “attachment” to my header:

Content-Disposition: attachment

Apparently this is a new security feature of FP 10 that keeps a SWF file from playing. I simply commented it out and I was good to go.

Speacial Thanks…

To my buddy Greg DeMelo for pointing me to this link.

Post to Twitter Tweet This Post