I have the following code and it does not work in Chrome if I keep the trigger function inside the confirm function but it works in Firefox and Edge.
fileInput = $('<input type="file" id="upload-file" name="timing-upload" style="display:none;"/>');
if (confirm("Are you sure you want to replace all files?")) {
$(fileInput).trigger('click');
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
But it works if we remove the confirm condition in Chrome.
fileInput = $('<input type="file" id="upload-file" name="timing-upload" style="display:none;"/>');
$(fileInput).trigger('click');
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
There is already a similar question Triggering a input file click does not bring the file upload dialog but not any satisfied answer.
Any help is highly appreciated. Thanks in advance.
I'm not sure but according to the confirm documentation I think that this is the reason that the code doesn't work in chrome:
Starting with Chrome 46.0 this method is blocked inside an <iframe> unless it sandbox attribute has the value allow-modal.
I am using ajax to submit a form that browses, uploads and then processes a file using PHP. I hate the standard html input type="file" button (as most people do) so used a trick to click the standard "browse" button from another button (id="fileSelect") that I can eventually customize. Works great in safari and others, but with IE the ajax code line that submits the form does not fire. Other elements of the ajax code are executed (I get the alert("Ajax complete"); at the end), but the "$("#imageform").ajaxForm({target: '#preview'}).submit();" line does not work in IE.
Ajax code:
$(document).ready(function()
{
$('#xfile').live('change', function() // when there is an even on the 'file' element (like file selected)
{
$("#preview").html(''); // clears the preview area
$("#preview").html('<img src="general_images/loading.gif" alt="Uploading...."/>'); // displays the loading
$("#imageform").ajaxForm({target: '#preview'}).submit();
alert ("Ajax complete");
});
});
And HTML Code:
<div>
<button id="fileSelect" onclick="document.getElementById('xfile').click();">Insert Picture</button>
<form style="display: inline" id="imageform" name="imageform" action="upload_file_journal.php" method="post" target="_blank" enctype="multipart/form-data">
<input style="visibility: hidden" type='file' name='xfile' id='xfile' /
</form>
This will not work in IE 9 and older(I think it works in IE 10,11).
The only real option is to use absolute positions to place your upload control on top of your button, then set the opacity to 0.1, then they look like they are clicking a button, but are actually clicking the input, you have to super size the file input to ensure they always click the button part.
Even doing that is a hassle though.
Hope this helps
I have the following HTML:
<form method="post" action="/link" style="position:absolute; left:-1000px;">
<input type="file" name="gameUpload" id="gameUpload" />
</form>
I am trying to integrate the upload feature in HTML into unity.
The idea is when a button is clicked in unity engine, it will make a call to a javascript function and the js function will trigger the click on the file input element.
This works perfectly fine in Firefox and IE10, IE9 and IE8. But it is not working in Chrome and Safari.
The JS code:
function uploadImage(){
jQuery("#gameUpload").trigger("click");
}
I tried having a "span" with some text and tried triggering the file input's click when that text is clicked, that worked fine.
Also, I did the following to check whether the click is triggered:
jQuery("#gameUpload").unbind("click").bind("click",function(){
alert("I am open");
});
Apparently, the click is getting triggered because I am getting the alert but the file dialog is not getting opened.
I'm not familiar with HTML in Unity, but I don't think you need the JS.
Just including the <input type="file" name="gameUpload" id="gameUpload" /> should allow the browser to open a dialog window.
With jQuery, just do this:
$('#imageUpload').click();
// or get the HTMLElement
$('#imageUpload').get(0).click();
Use html only (as we know, label with for attribute will focus the form element's default actions so.)
<label for="imageUpload">Upload File!</label>
<input id="imageUpload" type="file" style="display:none"/>
I have a html form, with a custom file upload field. And by that I mean that I have moved the actual file field beyond the borders of the page with css, that I have a custom input field and button in place, and that I have a jquery click event attached to that custom button to trigger the file input dialog.
It all works fine, in every browser.
But I need to submit the form through javascript. And I got somewhere that IE remembers my actions with javascript as a malicious manipulation of the file input field and blocks my access with an error "access denied" when I invoke document.formName.submit().
Is there a way around this, because I have gone completely mad by trying to search for a solution. I seriously don't want to use the default file input field, as every browsers renders it differently and messes up my design..
code:
<form name="thisForm" onsubmit="return false;" enctype="multipart/form-data" method="post" action="index.cfm/somepage">
<input type="file" class="hidden" name="hidden" id="hidden" />
<input type="text" name="shown" id="shown" />
<button id="button">browse..</button>
<input type="submit" id="submitForm" />
</form>
<script>
$('button').click(function(){
$('#shown').val($('#hidden').val());
});
$('submitForm').click(function(){
validateForm();
});
function validateForm()
{
//regular expression validation against all other input fields in the form
//not the file input field
validateVAT();
}
function validateVAT()
{
//connect to external service to check VAT
submitForm();
}
function submitForm()
{
document.thisForm.submit();
}
</script>
UPDATE:
I just tried to first upload the file, before submitting the form, through ajax, but that also gave me the acces denied error.. >_>
I was having the same problem, and I solved it by using a styled <label> tag with a slight workaround in Firefox.
http://jsfiddle.net/djibouti33/uP7A9/
The Goals:
allow user to upload a file by using standard html file input control
hide standard html file input control and apply own styling
after user selects file to upload, automatically submit the form
The Browsers:
Firefox, Chrome, IE8/9, Safari
IE7 didn't work, but it might if you add it to the workaround detailed below.
The Initial Solution:
Hide the file input by positioning it offscreen. Important not to display:none as some browsers won't like this.
Add another styled element to the page (link, button).
Listen for a click on that element, then programmatically send a click to the file input to trigger the native 'file explorer'
Listen for the file input's onchange event (occurs after a user chooses their file)
Submit the form
The Problem:
IE: if you programmatically send a click to a file input in order to activate it (2), programmatically submitting the form (5) will throw a security error
The Workaround Solution:
Same as above
Take advantage of the accessibility features built in to the label tag (clicking on a label will activate it's associated control) by styling
a label tag instead of a link/button
Listen for the file input's onchange event
Submit the form
For some reason Mozilla browsers won't activate a file input by clicking on it's label.
For Mozilla, listen for the click on the label and send a click event to the file input to activate it.
Hope this helps! Check out the jsfiddle for details on the html/js/css used to make it all work.
I found the answer myself, After 2 days of crazy trial&error. I hope I can help somebody with this..
I removed the hidden file input field from my coldfusion page and replaced it by an iframe tag. That iframe tag linked to another coldfusion page, containing another form with the removed file input field.
Now when I use javascript to click the file input field, which is still hidden from view, it still gives the browse file dialog without a hitch. But when I use javascript to submit the form, through the iframe, miraculously, it submits the form in the iframe, making it possible to upload the file in some serverside scripting of your preference.
iframe code:
<form id="formFileUpload" class="formFileUpload" name="formFileUpload" method="post" action="../actions/act_upload_file.cfm" autocomplete="off" enctype="multipart/form-data">
<input type="file" class="buttonFileHidden" id="inputFile" name="partnersLogo" />
</form>
iframe itself:
<iframe src="admin/dsp_file_upload.cfm" id="ifu" name="ifu" class="buttonFileHidden">
</iframe>
javascript click & submit:
ifu.document.formFileUpload.partnersLogo.click();
ifu.document.formFileUpload.submit();
If you're like me, and you don't want to use an iframe, and you weren't too keen on the label solution mentioned above, you can just position the original button above the styled button with an opacity of 0.
Using the example above, you would still have:
<input type="file" class="hidden" name="hidden" id="hidden" />
<input type="button" name="shown" id="shown" value="Add File" />
But .hidden would be defined like so:
.hidden {
position: absolute;
left: -150px;
opacity: 0;
filter: alpha(opacity=0);
}
Config: Set the opacity to 0.5 (or =50) to see the transparent element and tweak the left positioning.
Arguably just as hacky as the answers above, but a bootstrap-friendly solution, and in my case, the only one that worked.
I found a weird solution to solve this problem.
It thought about the js click thread. If it goes out of this thread, there no more security issues.
I chose to use window.setTimeout. see sample below:
<script type="text/javascript">
$(function () {
$("#<%= this.fuDoc.ClientID %>").bind('change', uploadFile);
$("#<%= this.btnUpload.ClientID %>").click(chooseFile);
});
function chooseFile() {
$("#<%= this.fuDoc.ClientID %>").click();
}
function uploadFile() {
var fu = $("#<%= this.fuDoc.ClientID %>");
if (fu.val() != "") {
window.setTimeout(function () {
<%= this.ClientScript.GetPostBackEventReference(this.btnUpload, "") %>;
}, 100);
}
}
</script>
<asp:FileUpload ID="fuDoc" runat="server" style="display: none;" />
<asp:Button runat="server" ID="btnUpload" Text="upload" OnClick="btnUpload_Click" />
<asp:Label ID="lbltext" Text="" runat="server" />`
then, no more acces denied!
This is an old post but the problem still arises. This may not be working because jQuery kindly fails silently. I was having this problem and wondering why my hidden form would not submit and the file get uploaded. I started off by using jQuery, but then I went vanilla. It still didn't work but looked as though an exception was being thrown in my .click() function.
Running
try {
document.getElementById('formid').submit();
} catch (e) {
alert(e);
}
showed that we indeed were throwing an error, and quick research showed that this was because IE DOES NOT SUPPORT SIMULATED CLICKS ON A FILE INPUT. This means that when the form went to be posted, IE would refuse to post the form
Excuse the bold caps, but I know many people will see text and not read it
Have you tried
$('button').click(function(){
$('form[name=thisForm]').submit()
});
You need to change onsumbit='return false;' to onsubmit='return validateForm()'.
Then have validateForm() return true if the form passes your validation checks, or false if it does not.
The onsubmit='return false' is preventing the form from submitting via document.thisForm.submit(); as well as when the user clicks the submit button.
I commented these lines in j query.form.js then every thing works fine for me. Don't ask me the reason even i don't have the solution for that but it works for sure.
if (io.contentWindow.document.execCommand) {
try { // #214
io.contentWindow.document.execCommand('Stop');
} catch(ignore) {}
}
im having trouble with a form submit script im using with javascript and jquery- its a form i have loaded into a facebook tab which when submit should pass a variable to another tab.
it works in ie9, chrome, ff and safari but client is stuck on ie 7 and 8. I dont know what the problem is- I have tried replace the button with a div and image incase .click wouldnt overwrite the submit function.
this should be a common problem with fix? or what am i doing thats completely wrong?
<script>
$('document').ready( function(){
$('#my_button').click(function (e) {
//this.form.onsubmit();
var_app = "444444";
url = "http://www.facebook.com/tab/?sk=bladadasd&app_data=" + var_app;
parent.window.location.replace(url);
e.preventDefault();
return false;
});
});
</script>
<form id="submit" method="POST" >
<ul>
<li><span>Photo code <sup class="required">*</sup></span><input id="barcode" name="barcode" type="text"/></li>
<li><span>First name</span><input id="firstName" name="firstName" type="text"/></li>
<li><span>Last name</span><input id="lastName" name="lastName" type="text"/></li>
<li><span>Email address</span><input id="optional_email" name="optional_email" type="text"/></li>
</ul>
<a class="help">Need help?</a>
<input value="" type="submit" id="my_button" />
</form>
edit the form bounces on to the correct page in normal browsers but in ie8 it just submits upon itself going nowhere and displaying the same again
If it is a submit button , then instead you should be working with onsubmit even on that form.
P.S. why is your var_app variable in global scope ?
Try the submit event instead of click event.
$('#my_button').submit(function(event) {
event.preventDefault();
});
You are also missing an action for your form.
I wonder if you should also specify the script type
Declare var_app & url variables.
var var_app = "444444";
var url = "http://www.facebook.com/tab/?sk=bladadasd&app_data=" + var_app;
The error lay with the line
parent.window.location.replace(url);
internet explorer only allowed the following statement and would throw a permission error
parent.location.replace(url);
nothing to do with jquery or my form syntax :( only realised this when i ran the code on a different machine in ie7