Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Input file - if not changed keep previous file attached

When user click on input['type="file"'] and select a file... file get attached. But if user click again on input and browse files but doesn't select one and close the dialog, the selected file disappears (input filed resets). Is there any way to prevent that?

like image 800
Uday Hiwarale Avatar asked Sep 03 '14 10:09

Uday Hiwarale


2 Answers

I'm pretty sure this is restricted by the browser as a security feature to prevent a user from uploading a file without first selecting it. I understand it was selected the first time but you can see how this can be used maliciously if we were able to set the value attribute or re-populate the input field after they hit "cancel" the second time.

like image 104
Eliel Avatar answered Nov 01 '22 15:11

Eliel


as Eliel said it is not recommended to do so for security reasons, Ex: The second time if you retain the path value but the file gets changed to a malicious one it is a purely insecure

But I show you how to retain the old path value here

var file_name = this.value;
$('input[type="file"]').on('change', function (event, files, label) {
    file_name = this.value;  
});

there is no direct way to find if cancel is clicked on dialog(Not exposed to browser)

But use this

document.body.onfocus = function(){
 document.getElementById('#fileInput').value = file_name;
} // to detect dialog closed

then the next time the dialog opened set the value to file_name (works Only in firefox with the below addon)

var pageMod = require('page-mod');
var self = require('self');

pageMod.PageMod({
    include: "url of app",
    contentScriptFile: [self.data.url('url of script file'), 
self.data.url('url of script file'),...]
});

Ref:https://forums.mozilla.org/addons/viewtopic.php?p=25153&sid=b6380f9e2acbf759e8833979561dd6f1

Hope it helps

like image 1
Khaleel Avatar answered Nov 01 '22 14:11

Khaleel