为了将臭名昭著的 bluimp jQuery 文件上传器集成到我的 AngularJS 项目中,我遵循了以下教程。
经过一番研究,我发现在选项数组中,在 jquery.fileuploader.js 文件中,有一个名为 autoUpload 的选项,当设置为 true 时会自动上传文件。我试图禁用它(假,未定义),但很快我就知道这会导致上传根本不起作用,甚至在表单提交时也不起作用。
我需要手动触发上传,比如在另一个回调中,或者通过点击事件。可以这样做吗?
代码:
app.directive("fileupload", function() {
return {
restrict: "A",
scope: {
done: "&",
progress: "&",
fail: "&",
uploadurl: "=",
customdata: "&"
},
link: function(scope, elem, attrs) {
var uploadOptions;
uploadOptions = {
url: scope.uploadurl,
dataType: "json"
};
if (scope.done) {
uploadOptions.done = function(e, data) {
return scope.$apply(function() {
return scope.done({
e: e,
data: data
});
});
};
}
if (scope.fail) {
uploadOptions.fail = function(e, data) {
return scope.$apply(function() {
return scope.fail({
e: e,
data: data
});
});
};
}
if (scope.progress) {
uploadOptions.progress = function(e, data) {
return scope.$apply(function() {
return scope.progress({
e: e,
data: data
});
});
};
}
return elem.fileupload(uploadOptions).bind("fileuploadsubmit", function(e, data) {
return data.formData = {
JSON.stringify(scope.customdata())
};
});
}
};
});
app.service('uploadService', function(authService) {
var initializeFn, processFn;
initializeFn = function(e, data, process) {
var upload;
return upload = {
message: '',
uploadurl: authService.getBaseUrl() + '/applications/',
status: false
};
};
processFn = function(e, data, process) {
var file, upload;
upload = {};
upload.successData = [];
upload.status = true;
upload.error = false;
if (process === 'done') {
upload.message = data.result.result;
console.log(data);
file = data.result.resume;
upload.successData = {
// name: file.name,
// fullUrl: file.url,
// thumbUrl: file.thumbnail_url
};
} else if (process === 'progress') {
upload.message = 'Uploading....!!!';
} else {
upload.error = true;
upload.message = 'Please try again';
console.log(e, data);
}
return upload;
};
return {
process: processFn,
initialize: initializeFn
};
});
app.controller('applyCtrl', function($scope, $routeParams, uploadService){
$scope.model.formData = {};
$scope.model.formData.job = $routeParams.jobId;
$scope.uploadLayer = function(e, data, process) {
return $scope.uploadReturn = uploadService.process(e, data, process);
};
$scope.customData = function() {
return $scope.model.formData;
};
return $scope.uploadReturn = uploadService.initialize();
});
看法:
<form class="applyForm" ng-submit="uploadLayer(e, data, 'progress')">
<fieldset>
<div class="formLine">
<div class="wideFieldContainer">
<input id="testUpload" type="file" fileupload customdata="customData()" name="resume" uploadurl="uploadReturn.uploadurl" done="uploadLayer(e, data, 'done')" fail="uploadLayer(e, data, 'fail')" progress="uploadLayer(e, data, 'progress')" />
</div>
</div>
</fieldset>
</form>
脚本加载顺序:
...
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="lib/angular/angular.js"></script>
<script src="lib/angular/angular-resource.js"></script>
<script src="js/app.js"></script>
<script src="js/services.js"></script>
<script src="js/controllers.js"></script>
<script src="js/filters.js"></script>
<script src="js/directives.js"></script>
<script src="lib/bluimp/js/vendor/jquery.ui.widget.js"></script>
<script src="lib/bluimp/js/jquery.piframe-transport.js"></script>
<script src="lib/bluimp/js/jquery.fileupload.js"></script>
</body>
</html>