Angular文件上传- cancelItem方法不起作用
Nervgh Angular File Upload - cancelItem method not working
这是我的html代码:
<input type="file" nv-file-select="" options="{ photoType: 'studentPic' }" uploader="uploader" />
这是angular代码:
var uploader = $scope.uploader = new FileUploader({
url: 'badges/photos',
autoUpload: true
});
// FILTERS
uploader.filters.push({
name: 'customFilter',
fn: function (item /*{File|FileLikeObject}*/ , options) {
return this.queue.length < 10;
}
});
// CALLBACKS
uploader.onWhenAddingFileFailed = function (item /*{File|FileLikeObject}*/ , filter, options) {
console.info('onWhenAddingFileFailed', item, filter, options);
};
uploader.onAfterAddingFile = function (fileItem) {
console.info('onAfterAddingFile', fileItem);
};
uploader.onAfterAddingAll = function (addedFileItems) {
console.info('onAfterAddingAll', addedFileItems);
};
uploader.onBeforeUploadItem = function (item) {
console.log("item before uploading:",item._file.type);
console.log("item before uploading:",item._file.size);
if((item._file.type!="image/png" && item._file.type!="image/jpeg") || item._file.size>102400){
console.log("in if of before");
this.cancelItem(item);
}
console.log(item.photoType);
var filename = generateUUID();
if (item.photoType === 'studentPic') {
$scope.pic = filename;
}
if (item.photoType === 'pickupPerson1Pic') {
$scope.pickupPerson1Pic = filename;
}
if (item.photoType === 'pickupPerson2Pic') {
$scope.pickupPerson2Pic= filename;
}
item.formData.push({
filename: filename
});
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) {
if (fileItem.photoType === 'studentPic') {
angular.element('#picPreview').attr("src", response.url);
}
if (fileItem.photoType === 'pickupPerson1Pic') {
angular.element('#pickupPerson1PicPreview').attr("src", response.url);
}
if (fileItem.photoType === 'pickupPerson2Pic') {
angular.element('#pickupPerson2PicPreview').attr("src", response.url);
}
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) {
console.info('onCompleteItem', fileItem, response, status, headers);
};
uploader.onCompleteAll = function () {
console.info('onCompleteAll');
};
我的上传。onBeforeUploadItem有条件if((item._file.type!)= "/png"形象,,item._file.type!="image/jpeg") || item._file.size>102400),如果成功,则调用this.cancelItem(item)方法。
uploader.onBeforeUploadItem = function (item) {
console.log("item before uploading:",item._file.type);
console.log("item before uploading:",item._file.size);
if((item._file.type!="image/png" && item._file.type!="image/jpeg") || item._file.size>102400){
console.log("in if of before");
this.cancelItem(item);
}
console.log(item.photoType);
var filename = generateUUID();
if (item.photoType === 'studentPic') {
$scope.pic = filename;
}
if (item.photoType === 'pickupPerson1Pic') {
$scope.pickupPerson1Pic = filename;
}
if (item.photoType === 'pickupPerson2Pic') {
$scope.pickupPerson2Pic= filename;
}
item.formData.push({
filename: filename
});
console.info('onBeforeUploadItem', item);
};
现在我是否错误地调用了cancel Item方法?因为尽管条件匹配,项目的上传仍然成功地继续。请帮助
this.cancelItem(item)将取消正在进行的上传。
你需要在上传文件之前检查条件。
例如:
<button type="button" id="upLoadFileButton" data-ng-click="validateStuffBeforeUpload(item)">Upload File</button>
然后在控制器中有一个函数用于验证想要验证的内容成功后调用uploadItem方法并传入文件item:
$scope.validateStuffBeforeUpload = function (fileItem) {
if((item._file.type!="image/png" && item._file.type!="image/jpeg") || item._file.size>102400){
console.log("in if of before");
} else {
uploader.uploadItem(fileItem);
}
};
或者其他的变化…在任何情况下,您都需要在调用uploadItem方法之前验证您想要的内容。
相关文章:
- electronic BrowserWindow的最小高度和宽度在hide()show()方法之后不起作用
- 有没有任何方法可以将控制器从文件加载到ui路由器$stateProvider中
- 数组在递归方法中设置为null
- 打破承诺链的好方法是什么
- 在使用Polymer'加载所有json文件后执行方法;s的核心ajax
- 使用“;这个“;JavaScript原型方法中的关键字
- 序列化数据属性中对象的最可靠方法
- 使用Objective-C的JavaScript注入方法
- 有没有一种方法可以添加相同的项目val=“0”;4〃;到JavaScript中数组的每个对象
- 有没有一种方法可以防止img get请求使用css或js发生
- Javascript,有没有一种方法可以将数组写成没有逗号或空格的单个文本字符串
- toBoolean方法类似于toString
- 如何在单击复选框后调用控制器方法
- 是否有任何方法可以使用jQuery替换在数组中定义值的文本
- 递归使用 eval() 是检查程序执行的好方法吗?
- 如何在webView,Android中从@JavascriptInterface方法调用Javascript
- toLocaleDateString和toLocaleString方法不尊重机器时区
- 有条件更新d3.js力图中节点的最佳方法
- TypeError:在不兼容的接收器nodejs上调用了方法Uint8Array.length
- Angular文件上传- cancelItem方法不起作用