Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
48
rated 0 times [  54] [ 6]  / answers: 1 / hits: 50267  / 13 Years ago, wed, february 22, 2012, 12:00:00

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.. >_>


More From » jquery

 Answers
67

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();

[#87298] Tuesday, February 21, 2012, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
rhiannab

Total Points: 370
Total Questions: 98
Total Answers: 100

Location: Samoa
Member since Mon, Nov 8, 2021
3 Years ago
;