开发者

In an HTTP file transfer, how do I set the name and type of the resource?

I'm building a proxy in Perl that checks if a user is allowed to access a specific file and if so, returns that file to the user. The user accesses the file by calling a URL of the form:

http://www.example.com/products?id=1234

If the user is authorized to access the file it should be returned to the user with the correct name and filetype. Here is the code I'm currently using to do this which doesn't seem to be working correctly:

my $file = "/path/to/file.tar.gz";
open FILE,qq|<$file| || die!;
print $cgi->header('application/x-gzip');
print <FILE>;
close FILE;

I'm sure there is something simple that I'm overlooking but how do I specify the name of the returned file and make sure the user's 开发者_JAVA百科browser treats is as the correct filetype? To be clear, I don't want the user to be able to access the file on the server directly but instead want the Perl script to read in and then serve the file back to the user.


You can set a Content-disposition header

Content-disposition: attachment; filename=fname.ext


die! is a syntax error. Replace your example code with:

use File::Slurp qw(read_file);
⋮
print $cgi->header(…);
my $file_content = read_file '/path/to/file.tar.gz'
print $file_content;

The problem should instead be solved more efficiently with using X-Sendfile.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜