【发布时间】:2014-01-14 01:13:58
【问题描述】:
这是我的代码: html:
<form enctype='multipart/form-data' name='biosUpload' method='POST' action='partials/uploadComplete.html' target='hiddenFrame' >
<input type='file' name={{romName}} id='brwsUpld' />
</form>
<button type="button" ng-click="uploadFwImage()">upload</button>
<iframe name="hiddenFrame" frameborder="0" iframe-upload id="uploadIframe"></iframe>
注意:uploadComplete.html 只是一个没有代码的 html。
js-控制器:
$scope.iframeFlag = false;
//submit form in $http's promise won't upload the file,
//just load uploadComplete.html into iframe,
//but when submit form outside the $http's promise ,it works.
//the problem is code follows worked ago, but just yesterday I found it can't , I am so confused.
//way1:
$http({
method: 'GET',
url: 'rpc/test.asp'
}).
success(function(data, status) {
$scope.iframeFlag = true;
document.forms["biosUpload"].submit();
}).
error(function(data, status) {});
//way2:
document.forms["biosUpload"].submit();
js 指令:
define(['./module'], function (directives) {
'use strict';
directives.directive('iframeUpload', function(){
var linkFn = function(scope, element, attrs) {
element.bind('load', function (event) {
console.log(scope.iframeFlag);
if(scope.isStop)return;
if(!scope.iframeFlag)return;
scope.uploadComplete();
});
};
return {
restrict: 'EA',
link : linkFn
};
});
});
问题: 当 way1 模式时,它只是将 uploadComplete.html 加载到 iframe 中。 在way2模式下,它也成功地将文件传输到网络服务器。 为什么 $http 会影响 load() 机制。 实际上,way1 之前工作过,就在昨天它失败了,并且再也没有工作过。
【问题讨论】:
-
尝试使用 $document 也许你会在其他地方覆盖文档
-
你能给我更详细的吗?
-
您可以在控制器或指令docs.angularjs.org/api/ng.$document 中注入文档,如果我没记错的话,您还可以将表单按名称传递给控制器,这可能是一个合适的解决方案docs.angularjs.org/api/ng.directive:form
标签: javascript forms angularjs upload submit