filehandle - Download file with Perl on form submit -
i trying initiate save dialog on form submit. form pretty simple, i'm using dropzonejs drag , drop file, , looks this:
<form action="action.epl" class="dropzone" id="dropzone" method="post"> </form>
so when user drops file submits , kicks off action.epl. in action.epl, handle file , gets saved server. i'm trying spit out encrypted version of file. encryption done , have removed make sure not source of problem, problem have can't download server. have following (also in action.epl):
$filename = 'file.pdf'; $filepath= "/server/path/$filename"; open (file, "<$filepath") or die "can't open : $!"; @fileholder = <file>; close file; print "content-type:application/x-downloadn"; print "content-disposition:attachment;filename=$filename"; print @fileholder
it's doing /something/ because submit takes 5x long did without snippet. thought "save as" dialog nothing happens. this tutorial got info.
edit, have:
$filename ='file.pdf'; $filepath = "/server/path/$filename"; print "content-type:application/x-download\n"; print "content-disposition:attachment;filename=$filename\n\n"; open file, '<', $filepath or die "can't open: $!"; print while <file>; close file;
however, there still no dialog. see have "$" sigil in filehandle. tried too. dont think need right?
i see addressed typo in tutorial.
"content-type:application/x-downloadn"
should "content-type: application/x-download\n"
, specify type of content "application/x-download" , "\n" end line of header field.
after that, you're getting how browser handles response. if provide content-disposition:attachment;filename=$filename
header, you're asserting attachment ought given filename $filename
. many browsers take peek @ file name, , try sniff suitable mime type extension. so, if you're specifying $filename
.pdf
modern browsers, "if looks pdf , smells pdf, it's pdf". not saying "this pdf" specification of content-disposition, you're providing name file download. in situations, should prevent fall-back "save as" behavior.
your best bet not provide content-disposition. way, you're not specifying default name save file as, , such there's no extension browser snoop. unfortunately, browsers default name of script if extension absurd compared contents. in of "enterprise solutions" deal in day-to-day, .csv files named "report.cgi" because use mime type internet explorer recognizes , don't provide content-disposition. buyer beware.
the bottom line can't force browser open "save as" server side unless have information browser , know how trick it, or don't give go (and browsers may have default conventions).
by specifying content-disposition , filename, you're giving hints file should be, , should saved as. on other hand, if don't give other hints browser other content-type 'application/x-download' you'll "save as" dialog box, user have no idea kind of content data is. puts @ mercy of browser's default naming conventions. how .csv files "report.cgi", when server providing mime type csv files (though ie-only flavor).
what use perl's file::type , mime_type function mime type , specify name. if use mime_type function determining mime type, , don't specify file return, you'll silly things .xlsx files being downloaded zip files, or other absurdity.
how important "save as" dialog box, because @ end of day, file type choose irrelevant if content of file not appropriate type , try open excel file in acrobat, or vice-versa.
in of years of experience doing server side programming, have found futile try control client side.
Comments
Post a Comment