lagouyn
lagouyn

Reputation: 376

How to change filename prompt text browser Save As dialog?

In my web page (rendered by Rails), I'd like to let the user right-click on a photo to bring up the browser's Save As dialog, to let the user save the photo to their hard drive.

However, the photos on my server have unusual filenames (long hex names) with no file extension. The filename prompt in the Save As dialog has this ugly filename. If the user hits save, they'll end up with a poorly-named file, with no file extension.

The web page is aware of the photo's real file name (the name that came off the camera, for example). Is there a way for me to programmatically override the Save As dialog's filename prompt with a filename of my choosing?

I'm aware of the Content-Dispostion header, and that via this header a filename can be specified. However, I think that in order to be able to make use of this header, I need to load/render the entire file to the browser. If the asset to be made available for download is a movie, that loading of the file could timeout the browser...like, if it's a 100meg video.

Thoughts?

-A

Upvotes: 6

Views: 1890

Answers (4)

cpm
cpm

Reputation: 1533

Based on your comments, you have a few problems.

  1. You want to set the filename using your Rails app.
  2. The file is on a remote host and your Rails app is acting as a middleman.
  3. The file might be big, so you want the file to be sent out to the browser as you receive it instead of queuing the whole thing.

Streaming only with Rails is tricky for a few reasons.

You would need an HTTP client that lets you access the message body as you receive data instead of blocking until you have everything. Net::HTTP is not that client. I'm not sure what library would be better suited.

Once you have a more event-driven way to get your file in pieces, you can pass a proc to the render: render :text => proc { |response, output| ... }

output can be used like an IO object. Some servers may buffer before sending anyway, though, so that's something to look out for.


It would be easier not handle the byte-shuffling in Rails.

If your webserver or the proxy in front of your webserver supports the X-REPROXY-URL HTTP header, your application can set that header and your webserver or proxy will stream the file.

Perlbal is the only proxy server I know of that supports that header out of the box.

An Apache2 module is also available.

Upvotes: 0

stephenmurdoch
stephenmurdoch

Reputation: 34613

I think I understand the problem here because I encountered (and resolved) at least part of it myself not too long ago.

I have some large mp3's and I link to them on my website

A few problems

  • I needed to set my content-disposition header to attachment in order to prevent files from automatically streaming whenever a user clicked the download button
  • my files are on a remote server
  • my files are large (100MB)
  • large files can tie up rails controllers if not handled properly

Now, Michael Koziarsky advises in this article that the best way to keep your rails processes free when serving large files, is to create a download action in your controller, and the do something like this (note the use of x_sendfile=>true):

  def download
    send_file '/path/to/podcast.mp3', :type => 'application/octet-stream', :disposition => 'attachment', :filename=>'something.mp3', :x_sendfile=>true
  end

:x_sendfile tells apache to let the file through without tying up a rails controller process. The rest of the code sets the filename and the content-disposition header.

Great, but I'm on heroku, like everyone else nowadays. So I can't use x_sendfile.

I found that I couldn't modify the nginx configuration file either as it's locked down by heroku so it was not possible to get x-accel-redirect (nginx equivalent of x-sendfile) working

So, I decided to add a perl script (see below) to the cgi-bin on our asset-host and this script sets the content-disposition to attachment and gives our file a name too.

Instead of doing a restful download like this:

link_to "download", download_podcast_path(@podcast.mp3)

we just link to the mp3 making sure that we go in through the cgi-bin so that the perl script gets called on every mp3 that leaves the server

# I'm using haml
%a{:href=>"http://afmpodcast.com/cgi-bin/download.cgi?ID=#{@podcast.mp3}"}
  download

The result is that my rails controller is no longer called into action when someone downloads a file

I found the perl script here and chopped it up a bit to work for me:

#!/usr/local/bin/perl -wT

use CGI ':standard';
use CGI::Carp qw(fatalsToBrowser);

my $files_location;
my $ID;
my @fileholder;

$files_location = "../";

$ID = param('ID');

open(DLFILE, "<$files_location/$ID") || Error('open', 'file');
@fileholder = <DLFILE>;
close (DLFILE) || Error ('close', 'file');

print "Content-Type:application/x-download\n";
print "Content-Disposition:attachment;filename=$ID\n\n";
print @fileholder

My code, is on github but you'll likely have all sorts of problems using it on your machine as i make heavy use of ENV variables that I store in bashrc and I have no documentation or tests ^hides^

Upvotes: 1

Nicolas78
Nicolas78

Reputation: 5144

Set the Content-Disposition to "attachment; filename="...that's fine. "attachment" explicitly means it's not to be rendered in the browser, file renaming works nonetheless (or possibly particularly for that case).

Upvotes: 0

Johan
Johan

Reputation: 5053

You could do some smart server side url rewrite, like for example rewriting foo.mpeg to youveryuglyfilenamewithoutextension.

Upvotes: 0

Related Questions