Question

Disclaimer I am aware of the Content-Disposition header to send back to the client to set the downloaded file name - however my problem is a little more complicated than just that

I have an application (RubyOnRails using rails 3.1.3) that is essentially a document search/view application (search for documents and then render them in the browser). This is accomplished using an iframe.

<iframe src="<%= @frameURL %>" width="100%" height="100%">

@frameURL is a call to the plugin function of our Documents controller. The plugin function makes a RESTful call to our back end API to retrieve the referenced document, and then send the document contents back to the browser for rendering inside the iframe.

This works perfectly for documents like JPEG, PDF, TXT, etc. However, when the browser does not know how to handle the content-type (like a word document - we run Mac OS-X) - then the browser downloads the returned file as plugin.doc <- NOTE this is without setting the Content-Disposition header.

Since we want to name the file appropriately when it needs to be downloaded, we set the Content-Disposition header:

response.headers['Content-Disposition'] = "attachment; filename.extension"

Now the file gets downloaded as filename.doc - however, with this header set, even files like JPEG which the browser can render internally, get downloaded.

Questions:

  1. Does anyone know where rails or the browser is getting the name of plugin.extension when we don't set the Content-Disposition header?

  2. Is there a way to set Content-Disposition but have it only applied IF the browser can't render the document - so the default should be browser handles everything it can, and as a fallback, the browser uses the Content-Disposition content to name the downloaded file.

Thanks!

Was it helpful?

Solution

  1. If you are calling some Rails function like "send_file", then search the source code of your version of Rails to find the source code of that function and see what headers it sets. You have to follow the call stack down a couple of levels but you should be able to find out how it sets the headers; I have done this before. As for the browser, I think if it doesn't find a file name in the Content-Disposition header it will more or less use the last portion of the URL for a filename.
  2. Try using "inline" instead of "attachment" in the header.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top