Setting Content-Disposition in s3_headers

792 views
Skip to first unread message

John Reilly

unread,
Aug 25, 2009, 2:05:05 PM8/25/09
to Paperclip Plugin
Hello,

I'm attempting to set a custom Content-Disposition header when
uploading files to s3. This is thanks to IE (/rollseyes) not knowing
how to download files with a content-type of application/octet-stream.
All other browsers are fine, but IE requires a 'Content-Disposition:
attachment; filename="file.exe"' header for these file types.

After adding this header manually to my previously uploaded files in
s3, IE behaves correctly.

So, I want to set this header when the file is uploaded via paperclip,
however, I can't figure out how to specify the file's name in the
header.

Here's my has_attached_file parameters:

has_attached_file :data,
:storage => :s3,
:s3_credentials => ...,
:s3_permissions => ...,
:path => ...,
:bucket => ...,
:s3_headers => {"Content-Disposition" =>
"attachment; filename=\"#{FILE NAME GOES HERE}\""}

Does anyone have any suggestions on how to put the correct uploaded
file name into this header? I've been fighting with it so long that I
can't see straight anymore. :-)

Thanks in advance for any advice!

-John Reilly

John Reilly

unread,
Aug 25, 2009, 3:10:55 PM8/25/09
to Paperclip Plugin
On Aug 25, 1:05 pm, John Reilly <john.j.rei...@gmail.com> wrote:
> Does anyone have any suggestions on how to put the correct uploaded
> file name into this header?  

One thing I've tried is to add paperclip interpolations into the
values of the s3_headers hash. I think this would be a good solution
to my problem.

Here's a commit on my fork:
http://github.com/johnreilly/paperclip/commit/bfe12b8a48ebdbbb08cdc9705835e11b1dca6660

I think this works, but I'm having problems getting the test to pass.
Specifically, using the :filename interpolation seems to break for
some reason, but if I try a different interpolation
(say, :attachment), it works as expected. I think I'm having trouble
mocking up the file object, which causes the :basename and :extname
interpolations to break.

Any suggestions on what I'm doing wrong in the test? Is this a good
solution to my problem, or do I need to go back to the drawing
board? :)

Thanks again,

-John Reilly

krismartin

unread,
Sep 30, 2009, 11:17:51 PM9/30/09
to Paperclip Plugin
Hi John,

I needed to do the same and I managed to get it working by setting the
content disposition after the file is uploaded to S3.

Here is a snippet of my code:

# photo.rb
has_attached_file :data, ...

def set_attachment_content_disposition(style, file_name)
path = self.data.path(style)
bucket_name = self.data.bucket_name
obj = AWS::S3::S3Object.find(path, bucket_name)
obj.content_disposition = 'attachment; filename="' + file_name +
'"'
obj.store
end

The code above is using Paperclip 2.3.1 and AWS/S3 gem.

Cheers,
Kris


On Aug 26, 5:10 am, John Reilly <john.j.rei...@gmail.com> wrote:
> On Aug 25, 1:05 pm, John Reilly <john.j.rei...@gmail.com> wrote:
>
> > Does anyone have any suggestions on how to put the correct uploaded
> > file name into this header?  
>
> One thing I've tried is to add paperclip interpolations into the
> values of the s3_headers hash.  I think this would be a good solution
> to my problem.
>
> Here's a commit on my fork:http://github.com/johnreilly/paperclip/commit/bfe12b8a48ebdbbb08cdc97...
Reply all
Reply to author
Forward
0 new messages