我试图通过文件上传修复IE9中的问题。我正在使用JQuery的文件上传插件,当我默认使用输入文件时,我没有问题,但是如果我使用隐藏的输入文件并通过点击按钮来触发事件,那就不行了。
我使用两个指令来实现这个目标:
ecm_directives.directive('fileupload', function(){
return {
restrict: 'A',
scope: {
done: '&',
progress: '&'
},
link: function(scope, element, attrs) {
var optionsObj = {
dataType: 'json'
};
if (scope.done) {
optionsObj.done = function(e, data) {
scope.$apply(function() {
scope.done({e: e, data: data});
});
};
}
if (scope.progress) {
optionsObj.progress = function(e, data) {
scope.$apply(function() {
scope.progress({e: e, data: data});
});
};
}
//the above could easily be done in a loop, to cover
//all API's that Fileupload provides
element.fileupload(optionsObj);
}
}
});
ecm_directives.directive('uploader', function () {
return {
restrict: 'E',
link: function(scope, element, attrs,ctrl) {
element.find("button").bind("click", function(){
element.find(":file").trigger("click").bind("change",function(){
console.log("change");
scope.kaka = element.find("input").val();
scope.$apply();
console.log("changed", scope.kaka);
})
});
}
}
});
这是html
<div class="fake-file" ng-show="!candidateFiles.cedula.selected">
<uploader>
<button type="button" class="fake-input">
<span class="placeholder ng-binding"> Seleccione un archivo de su ordenador</span>
</button>
<button type="button" class="btn fake-btn fake-upload-btn ng-binding">Subir</button>
<input ng-model="ccedulaCandidato" id="cedula-file" type="file" name="ccedulaCandidato" data-url="/backend/documents/save/" class="file_upload_btn ng-isolate-scope ng-scope ng-pristine ng-valid" style="display:none" fileupload="" done="uploadFinished(e, data)">
</uploader>
</div>
重点是: element.find(&#34;:文件&#34)。绑定(&#34;变更&#34;,函数(){ }
永远不要进入IE9,任何想法?
答案 0 :(得分:0)
此代码在IE9中无效,请参阅IE9 file input triggering using Javascript
element.find(":file").trigger("click")
但是如果您添加这样的控制台日志,您将看到按钮单击事件正常工作。
element.find("button").bind("click", function(){
console.log('I'm in);
element.find(":file").trigger("click").bind("change",function(){