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?
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With