angularJs上傳文件-以及遇到的坑

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

接下說下我在項目中實際用到的文件上傳!不周到的地方還望指教!!!

待續。。。

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章