angularJs上传文件使用的是angular-file-upload控件。
先介绍一下这个控件的一些属性和一些方法;
FileUploader
属性
url {String}: 上传文件的服务器路径
alias {String}: 包含文件的名称,默认是file
queue {Array}: 上传队列
progress {Number}: 上传队列的进度,只读
headers {Object}: 上传的头文件信息, 浏览器需支持HTML5
formData {Array}: 与文件一起发送的表单数据
filters {Array}: 在文件加入上传队列之前应用过滤器.,如果过滤器返回true则文件加入队列中
autoUpload {Boolean}: 文件加入队列之后自动上传,默认是false
method {String}: 请求方式,默认是POST,浏览器需支持HTML5
removeAfterUpload {Boolean}: 文件上传成功之后从队列移除,默认是false
isHTML5 {Boolean}: 如果浏览器支持HTML5上传则返回true,只读
isUploading {Boolean}: 文件正在上传中返回true,只读
queueLimit {Number} : 最大上传文件数量(预定义)
withCredentials {Boolean} : 使用CORS,默认是false, 浏览器需支持HTML5
方法
addToQueue function(files[, options[, filters]]) {: Add items to the queue, where files is a {FileList|File|HTMLInputElement}, options is an {Object} andfilters is a {String}. 添加项到上传队列中,files 是 {FileList|File|HTMLInputElement}, options 是 {Object} 以及 filters 是 {String}
removeFromQueue function(value) {: Remove an item from the queue, wherevalue is {FileItem} or index of item. 从上传队列移除项,value 可以是 {FileItem} 或者项的序号
clearQueue function() {: Removes all elements from the queue. 移除上传队列所有的元素
uploadItem function(value) {: Uploads an item, where value is {FileItem} or index of item. 上传项, value 可以是 {FileItem} 或者项的序号
cancelItem function(value) {: Cancels uploading of item, where value is{FileItem} or index of item. 取消上传的项
uploadAll function() {: Upload all pending items on the queue. 将上传队列中所有的项进行上传
cancelAll function() {: Cancels all current uploads. 取消所有当前上传
destroy function() {: Destroys a uploader.
isFile function(value) {return {Boolean};}: Returns true if value is {File}.
isFileLikeObject function(value) {return {Boolean};}: Returns true if value is{FileLikeObject}.
getIndexOfItem function({FileItem}) {return {Number};}: Returns the index of the{FileItem} queue element. 返回项在上传队列中的序号
getReadyItems function() {return {Array.<FileItems>};}: Return items are ready to upload. 返回准备上传的项
getNotUploadedItems function() {return {Array.<FileItems>};}: Return an array of all pending items on the queue 返回上传队列中未上传的项
回调函数
onAfterAddingFile function(item) {: 添加文件到上传队列后
onWhenAddingFileFailed function(item, filter, options) {: 添加文件到上传队列失败后
onAfterAddingAll function(addedItems) {: 添加所选的所有文件到上传队列后
onBeforeUploadItem function(item) {: 文件上传之前
onProgressItem function(item, progress) {: 文件上传中
onSuccessItem function(item, response, status, headers) {: 文件上传成功后
onErrorItem function(item, response, status, headers) {: 文件上传失败后
onCancelItem function(item, response, status, headers) { - 文件上传取消后
onCompleteItem function(item, response, status, headers) {: 文件上传完成后
onProgressAll function(progress) {: 上传队列的所有文件上传中
onCompleteAll function() {: 上传队列的所有文件上传完成后
成功上传文件的回调函数顺序是:onAfterAddingFile — onAfterAddingAll — onBeforeUploadItem — onProgressItem — onProgressAll — onSuccessItem — onCompleteItem — onCompleteAll
FileItem
属性
url {String}: Path on the server in which this file will be uploaded 上传文件的服务器路径
alias {String}: Name of the field which will contain the file, default is file 包含文件的名称,默认是file
headers {Object}: Headers to be sent along with this file. HTML5 browsers only. 上传的头文件信息, 浏览器需支持HTML5
formData {Array}: Data to be sent along with this file 与文件一起发送的表单数据
method {String}: It's a request method. By default POST. HTML5 browsers only. 请求方式,默认是POST,浏览器需支持HTML5
withCredentials {Boolean} : enable CORS. HTML5 browsers only. 使用CORS,默认是false, 浏览器需支持HTML5
removeAfterUpload {Boolean}: Remove this file from the queue after uploading 上传之后从上传队列移除该文件
index {Number} - A sequence number upload. Read only. 上传文件在上传队列中的序号,只读
progress {Number}: File upload progress percentage. Read only. 文件上传的进度,只读
isReady {Boolean} - File is ready to upload. Read only. 文件是否准备好上传,只读
isUploading {Boolean}: true if the file is being uploaded. Read only. 文件是否正在上传中,只读
isUploaded {Boolean}: true if the file was uploaded. Read only. 文件是否已经上传,只读
isSuccess {Boolean}: true if the file was uploaded successfully. Read only. 文件是否已经上传成功,只读
isCancel {Boolean} : true if uploading was canceled. Read only. 文件是否取消上传,只读
isError {Boolean} - true if occurred error while file uploading. Read only. 文件是否上传错误,只读
uploader {Object}: Reference to the parent Uploader object for this file. Read only. 上传该文件的Uploader ,只读
方法
remove function() {: Remove this file from the queue 从上传队列移除该文件
upload function() {: Upload this file 上传该文件
cancel function() {: Cancels uploading of this file 取消上传该文件
回调函数
onBeforeUpload function() {: Fires before uploading an item. 上传该文件之前
onProgress function(progress) {: On file upload progress. 上传该文件的过程
onSuccess function(response, status, headers) {: On file successfully uploaded 成功上传该文件后
onError function(response, status, headers) {: On upload error 上传该文件出错后
onCancel function(response, status, headers) { - On cancel uploading 取消上传该文件后
onComplete function(response, status, headers) {: On file upload complete (independently of the sucess of the operation) 完成上传该文件后
Filters(过滤器)
注册过滤器:
var uploader =newFileUploader({
filters: [{
name:'yourName1',
// A user-defined filterfn:function(item) {
returntrue;
}
}]
});
转载连接:https://www.cnblogs.com/xfdmb/p/6284899.html
接下说下我在项目中实际用到的文件上传!不周到的地方还望指教!!!
先看下实际效果:
html文件代码:
Css样式我就不贴了,你们可以自己定义!
<div class="modal-header">
<button type="button" class="close" ng-click="cancel()">×</button>
<h4 class="modal-title fn-ms">{{title}}</h4>
</div>
<div class="panel-body">
<a href="javascript:;" target="_blank" rel="external nofollow" class="file_a" >
<input type="file" name="file" nv-file-select="" uploader="uploader" multiple />选择文件
</a>
<div class="col-md-12" style="margin-bottom: 20px">
<h3>文件上传队列</h3>
<p>队列长度: {{ uploader.queue.length }}</p>
<table class="table" >
<thead>
<tr>
<th width="50%">文件名</th>
<th ng-show="uploader.isHTML5">文件大小</th>
<th ng-show="uploader.isHTML5">进度</th>
<th>状态</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="item in uploader.queue">
<td><strong>{{ item.file.name }}</strong></td>
<td ng-show="uploader.isHTML5" nowrap>{{ item.file.size/1024/1024|number:2 }} MB</td>
<td ng-show="uploader.isHTML5">
<div class="progress" style="margin-bottom: 0;">
<div class="progress-bar" role="progressbar" ng-style="{ 'width': item.progress + '%' }"></div>
</div>
</td>
<td class="text-center">
<span ng-show="item.isSuccess"><i class="glyphicon glyphicon-ok"></i></span>
<span ng-show="item.isCancel"><i class="glyphicon glyphicon-ban-circle"></i></span>
<span ng-show="item.isError"><i class="glyphicon glyphicon-remove"></i></span>
</td>
<td nowrap>
<button type="button" class="btn btn-success btn-xs" ng-click="item.upload()" ng-disabled="item.isReady || item.isUploading || item.isSuccess">
<span class="glyphicon glyphicon-upload"></span> 上传
</button>
<button type="button" class="btn btn-warning btn-xs" ng-click="item.cancel()" ng-disabled="!item.isUploading">
<span class="glyphicon glyphicon-ban-circle"></span> 关闭
</button>
<button type="button" class="btn btn-danger btn-xs" ng-click="item.remove(this)">
<span class="glyphicon glyphicon-trash"></span> 删除
</button>
</td>
</tr>
</tbody>
</table>
<div>
<div>
进度:
<div class="progress" style="">
<div class="progress-bar" role="progressbar" ng-style="{ 'width': uploader.progress + '%' }"></div>
</div>
</div>
<button type="button" class="btn btn-success btn-s" ng-click="uploader.uploadAll()" ng-disabled="!uploader.getNotUploadedItems().length">
<span class="glyphicon glyphicon-upload"></span>上传所有
</button>
<button type="button" class="btn btn-warning btn-s" ng-click="uploader.cancelAll()" ng-disabled="!uploader.isUploading">
<span class="glyphicon glyphicon-ban-circle"></span>关闭所有
</button>
<button type="button" class="btn btn-danger btn-s" ng-click="uploader.clearQueue()" ng-disabled="!uploader.queue.length">
<span class="glyphicon glyphicon-trash"></span> 删除所有
</button>
</div>
</div>
</div>
<div class="btn-group dropup" style="float: right">
</div>
<div class="panel-footer" style="text-align:center">
<button type="button" class="btn btn-primary fn-ms" ng-click="submit()">提交</button>
<button type="button" class="btn btn-primary fn-ms" ng-click="cancel()">关闭</button>
</div>
重点说下js里面的代码吧。
var uploader = $scope.uploader = new FileUploader({
//最重要的!!!文件流会出现在这里,所以这里就调用后台的上传文件服务,将文件流传过去,详细的FileUploader属性上面有介绍。
});
// 回调函数,这里的回调函数上面也有介绍。
uploader.onWhenAddingFileFailed = function(item /*{File|FileLikeObject}*/, filter, options) {
console.info('onWhenAddingFileFailed', item, filter, options);
};
uploader.onAfterAddingFile = function(fileItem) {
//添加一个文件到文件队列就会调用这个方法
};
uploader.onAfterAddingAll = function(addedFileItems) {
//文件全部加入队列后,就会调用这个方法
console.info('onAfterAddingAll', addedFileItems);
};
//下面的一些目前项目中用到的不多,其实就是你想实现什么功能,在上传文件的某一个时刻对应的那个回调函数写下你的逻辑就好
//比如你想上传文件里面不能有重复的,你可以在过滤器里面写下你的逻辑,你也可以在每一个上传文件后的那个回调函onAfterAddingFile//这里写下你的去重逻辑代码,过滤器代码我就没贴了,里面的一些具体逻辑还是自己实现比较好。
uploader.onBeforeUploadItem = function(item) {
console.info('onBeforeUploadItem', item);
};
uploader.onProgressItem = function(fileItem, progress) {
console.info('onProgressItem', fileItem, progress);
};
uploader.onProgressAll = function(progress) {
console.info('onProgressAll', progress);
};
uploader.onSuccessItem = function(fileItem, response, status, headers) {
console.info('onSuccessItem', fileItem, response, status, headers);
};
uploader.onErrorItem = function(fileItem, response, status, headers) {
console.info('onErrorItem', fileItem, response, status, headers);
};
uploader.onCancelItem = function(fileItem, response, status, headers) {
console.info('onCancelItem', fileItem, response, status, headers);
};
uploader.onCompleteItem = function(fileItem, response, status, headers) {
};
uploader.onCompleteAll = function(response) {
};
上面那些回调函数,不太了解他的一些参数什么的,你也可以像上面写的那样讲那些参数在控制台里面打印出来,这样理解起来就会清晰一些。
讲解的不是太好,如果有什么问题,希望可以一起讨论 [email protected]