Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

format.xlsx change filename? #4

Closed
cj opened this Issue · 9 comments

3 participants

@cj
cj commented

Hi,

Thank you for this great gem :) Question, using format.xlsx how would you change the filename?

Many thanks,

@straydogstudio

You're welcome.

This should do it:

  format.xlsx {
    response.headers['Content-Disposition'] = "attachment; filename=\"filename.xlsx\""
  }

I've just tested this with rspec, but I've not done it live. Let me know if this works for you and I'll close the issue. Perhaps later we can come up with something prettier.

@cj
cj commented

@straydogstudio thank you for the quick response. I tried that but the name still remains the same.... any other ideas?

@straydogstudio

Which browser are you using?

@cj
cj commented

Chrome 21.0.1180.79 (mac)

@straydogstudio

I think there are two issues. One is the suggested file name, and the second is getting the browser to ask where you would like to save the file.

As for the first, in all of my tests, with Chrome, Firefox, Safari, and IE 8, the filename given is successfully used (even if there is no 'Save As...' dialog.) If this doesn't work for you, let me know.

The 'Save As...' dialog appears to be an issue with Chrome and Safari. For Safari, I've not found a setting to change this. For Chrome, an advanced setting lets you turn on the dialog, but if it is unchecked, nothing I've tried will bring it up, including changing the type of the file to 'application/octet-stream.' Firefox and IE, however, do proffer the dialog. Of course, you can always right click on the link and choose the save as... menu item.

Incidentally, I find the following format to be more readable:

  format.xlsx {
    render xlsx: "template_name", disposition: "attachment", filename: "filename_test.xlsx"
  }

At this point I'm not sure what I can do to improve the situation. Browsers will handle the disposition header in their own ways. Any ideas are welcome.

@asanghi

Just noticed that

format.xlsx {
    render xlsx: "template_name", disposition: "attachment", filename: "filename_test.xlsx"
  }

did not work, but the below did

format.xlsx {
    response.headers['Content-Disposition'] = "attachment; filename=\"filename.xlsx\""
  }
    render xlsx: "template_name", disposition: "attachment", filename: "filename_test.xlsx"

works when NOT within the respond_to block

Using Rails 3.2.12/Ruby 1.9.3

@straydogstudio

Thanks. I'll take a look when I can.

@straydogstudio

My tests are not failing with 3.2.12. It still sets the headers. What is the 'failure' you are seeing? Is the browser not getting the file name? Can you use Chrome dev tools to see the header differences on the requests?

I do notice there are some header differences:

  • Using response.headers adds "; charset=utf-8" to the Content-Type
  • Render xlsx adds "Content-Transfer-Encoding" = "binary"
  • Using response.headers sets "Cache-Control" as "max-age=0, private, must-revalidate", where it is just "private" for render xlsx.

None of these ought to break the request. Let me know what details you can.

@straydogstudio

I should also add I am not seeing any differences in output between render xlsx: inside a format.xlsx block and by itself as a standalone statement. That leaves me wondering why your request breaks inside the block.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.