Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Upload file to HTML5 page using keyboard shortcuts

I know that it is possible to upload files using drag and drop with the HTML5 File API. Can this also be done by copying a file from the file explorer and paste it into a web page using CTRL-V/CMD-V or by pasting from the right click context menu?

like image 851
Jørgen Avatar asked Aug 29 '12 08:08

Jørgen


People also ask

What is the shortcut key to insert a file?

Tip: To quickly create a new blank file or email, press Ctrl+N.

How do I use keyboard shortcuts in HTML?

You can add keyboard shortcuts to your page through HTML with the global accesskey attribute and trigger it with Alt + Shift + key in Chrome and Firefox on Windows, or use Control + Alt + key on Mac.

What is the meaning of Ctrl A to Z?

Ctrl + A → Select all content. Ctrl + Z → Undo an action. Ctrl + Y → Redo an action. Ctrl + D → Delete the selected item and move it to the Recycle Bin.


1 Answers

You can't do that.

You can get the file path easily with this :

$(document).on('paste',function(e){
    var path = e.originalEvent.clipboardData.getData("text");
});​

So you may show it to the user.

But you can't change yourself the value of an <input type=file>.

That's a security measure : imagine if your script could change the path of the file to be uploaded just before the user submits the form (or even without user interaction as is now possible with other form elements) ? As for every important security protection, there is no known "workaround" for modern browsers.

like image 118
Denys Séguret Avatar answered Nov 03 '22 20:11

Denys Séguret