Reputation: 4682
I have stylized My File input using :
<html>
<head>
<style>
#yourBtn{
position: relative;
top: 150px;
font-family: calibri;
width: 150px;
padding: 10px;
border-radius: 5px;
border: 1px dashed #ddeeff;
text-align: center;
background-color: #ffddee;
cursor:pointer;
color:#333333;
}
</style>
<script type="text/javascript">
function getFile(){
document.getElementById("upfile").click();
}
function sub(obj){
document.getElementById("yourBtn").innerHTML = "Uploading Please Wait...";
document.myForm.submit();
event.preventDefault();
}
</script>
</head>
<body>
<center>
<form action="/sub/upload.php" method="POST" enctype="multipart/form-data" name="myForm" target="myiframe">
<div id="yourBtn" onclick="getFile()">click to upload a file</div>
<div style='height: 0px;width: 0px; overflow:hidden;'><input id="upfile" type="file" name="upload" style="display:none;" onchange="sub(this)" /></div>
</form>
<iframe name="myiframe" style="width:100;height:100;border:2px solid #bbccdd;"></iframe>
</center>
</body>
</html>
The Problem is .submit() function returns the ' Access Denied ' error on IE (tested in IE8 and 9). Works fine in all other browsers (chrome,opera,safari and Firefox). when the sub(obj) is invoked. And The same is happening even if i use Jquery also.
So Can any one tell me how should i get this script working on IE ?
Upvotes: 6
Views: 13250
Reputation: 12508
As noted above IE does not allow you to open the dialog and submit a file on the user's behalf through javascript. That being said, your idea of styling the "file input" is completely valid.
This link may help you with that:
It's a buggy work around to say the least but the general gist is this:
Write javascript to pull the value of the file input (i.e. in jQuery
$('input[type="file"]').val();
and place it in the text input.
The idea here is that the file input is still pulling the file and the user is still choosing the file. You're in a sense, masking it and making it appear as if you have a custom control. Really, your fake control is behind the real one which is simply transparent.
Hope this helps
Upvotes: 5
Reputation: 19890
This will never work. IE does not allow you to invoke the "choose file" dialog via javascript AND submit the associated form via javascript. If you attempt this, as you have seen, IE will throw an error on form submission. You must allow the user to click the input element themselves.
Upvotes: 0