Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
88
rated 0 times [  92] [ 4]  / answers: 1 / hits: 19729  / 11 Years ago, thu, january 9, 2014, 12:00:00

I'm not sure if the place to ask this question so feel free to move my question elsewhere or closed it.

I have been told that Javascript can write and read from disk only when an explicit event like drag & drop happen on the browser and gave it a handle to a file.



In the case of Mega(ex upload) when you click download you are directly saving a file to disk without being asked to save it or installing any extension your browser.

Is it a feature of Javascript or HTML 5 because in my opinion it means a terrible security issue in this case.



Edit 1:
So my question is, how do Mega manage to write on disk without showing you the Save to dialog of your browser popped up and self determining where to put the file


More From » php

 Answers
58

It's just a matter of sending the right headers in the response - so the browser knows to save instead of open. It's nothing to do with javascript or HTML5.



For example, in PHP:



header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($file).'');
header('Content-Transfer-Encoding: binary');
header('Connection: Keep-Alive');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($file));


I don't know what server-side technology Mega uses but this is the basic premise.



Edit: in response to your edited question about why the 'Save As' dialog does not appear, well, that's a browser setting. Chrome, for example, will by default save to your Downloads folder (on Windows) without prompting.


[#73288] Wednesday, January 8, 2014, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
emerald

Total Points: 547
Total Questions: 96
Total Answers: 122

Location: Oman
Member since Fri, Dec 23, 2022
1 Year ago
;