溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

angularJS上傳文件

發(fā)布時(shí)間:2020-03-02 10:41:21 來源:網(wǎng)絡(luò) 閱讀:544 作者:紫楓5966 欄目:移動(dòng)開發(fā)

<div class="col-lg-1 col-sm-1 pull-right">
                                    <a id="fileb" href="javascript:;" name="file" ng-show="jIn">
                                        <input type="file" nv-file-select="" uploader="uploader1" multiple /><a class="btn btn-info  btn-sm" ng-show="jIn">導(dǎo)入</a>
                                    </a>
                                </div>


//導(dǎo)入
    var uploader1 = $scope.uploader1 = new FileUploader({
        url: 'http://localhost:8080/importWorkBook?loanChannel=005',
        autoUpload:true
    });

    // FILTERS

    uploader1.filters.push({
        name: 'customFilter',
        fn: function(item /*{File|FileLikeObject}*/, options) {
            return this.queue.length < 10;
        }
    });

    // CALLBACKS

    uploader1.onWhenAddingFileFailed = function(item /*{File|FileLikeObject}*/, filter, options) {
//        console.info('onWhenAddingFileFailed', item, filter, options);
    };
    uploader1.onAfterAddingFile = function(fileItem) {
//        console.info('onAfterAddingFile', fileItem);
    };
    uploader1.onAfterAddingAll = function(addedFileItems) {
//        console.info('onAfterAddingAll', addedFileItems);
    };
    uploader1.onBeforeUploadItem = function(item) {
//        console.info('onBeforeUploadItem', item);
    };
    uploader1.onProgressItem = function(fileItem, progress) {
//        console.info('onProgressItem', fileItem, progress);
    };
    uploader1.onProgressAll = function(progress) {
//        console.info('onProgressAll', progress);
    };
    uploader1.onSuccessItem = function(fileItem, response, status, headers) {
        if(response.returnMsg == "ok"){
            swal({
                title : "導(dǎo)入成功",
                text : "",
                type : "success",
                timer: 1300,
                showConfirmButton: false
                });
            $scope.processingInfo();
        }else{
            swal({
                title : "導(dǎo)入失敗",
                text : "對不起,數(shù)據(jù)有誤,導(dǎo)入失敗!",
                type : "error",
                timer: 1300,
                showConfirmButton: false
                });
            $scope.processingInfo();
        }
//        console.info('onSuccessItem', fileItem, response, status, headers);
    };
    uploader1.onErrorItem = function(fileItem, response, status, headers) {
//        console.info('onErrorItem', fileItem, response, status, headers);
    };
    uploader1.onCancelItem = function(fileItem, response, status, headers) {
//        console.info('onCancelItem', fileItem, response, status, headers);
    };
    uploader1.onCompleteItem = function(fileItem, response, status, headers) {
//        console.info('onCompleteItem', fileItem, response, status, headers);
    };
    uploader1.onCompleteAll = function() {
//        console.info('onCompleteAll');
    };

//    console.info('uploader', uploader);
 

控制多文件上傳及文件類型

 accept="p_w_picpath/*"

 multiple



Directives(指令)

  • nv-file-drop

<!-- 最少配置 --><elementnv-file-dropuploader="{FileUploader}"></element><!-- 最多配置 --><elementnv-file-dropuploader="{FileUploader}"options="{Object}"filters="{String}"></element>

屬性 uploader 必須是 FileUploader的一個(gè)實(shí)例。
屬性 options 可能是 {FileItem} 的設(shè)置或者自定義設(shè)置。
屬性 filters 可能是用,隔開的過濾器名稱,比如: 'filterName1, filterName2',這些過濾器必須如下預(yù)定義:

uploader.filters.push({name:'filterName1', fn:function() {/* your code here */}});
uploader.filters.push({name:'filterName2', fn:function() {/* your code here */}});
  • nv-file-select

<!-- 最少配置 --><inputtype="file"nv-file-selectuploader="{FileUploader}"/><!-- 最多配置 --><inputtype="file"nv-file-selectuploader="{FileUploader}"options="{Object}"filters="{String}"/>
  • nv-file-over

<!-- 最少配置 --><elementnv-file-overuploader="{FileUploader}"></element><!-- 最多配置 --><elementnv-file-overuploader="{FileUploader}"over-class="{String}"></element>

Service(服務(wù))

  • FileUploader

FileUploader

屬性

  • url {String}: 上傳文件的服務(wù)器路徑

  • alias {String}:  包含文件的名稱,默認(rèn)是file

  • queue {Array}: 上傳隊(duì)列

  • progress {Number}: 上傳隊(duì)列的進(jìn)度,只讀

  • headers {Object}: 上傳的頭文件信息, 瀏覽器需支持HTML5

  • formData {Array}: 與文件一起發(fā)送的表單數(shù)據(jù)

  • filters {Array}: 在文件加入上傳隊(duì)列之前應(yīng)用過濾器.,如果過濾器返回true則文件加入隊(duì)列中

  • autoUpload {Boolean}: 文件加入隊(duì)列之后自動(dòng)上傳,默認(rèn)是false

  • method {String}: 請求方式,默認(rèn)是POST,瀏覽器需支持HTML5

  • removeAfterUpload {Boolean}: 文件上傳成功之后從隊(duì)列移除,默認(rèn)是false

  • isHTML5 {Boolean}如果瀏覽器支持HTML5上傳則返回true,只讀

  • isUploading {Boolean}文件正在上傳中返回true,只讀

  • queueLimit {Number} : 最大上傳文件數(shù)量(預(yù)定義)

  • withCredentials {Boolean} : 使用CORS,默認(rèn)是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}.  添加項(xiàng)到上傳隊(duì)列中,files 是 {FileList|File|HTMLInputElement}, options 是 {Object} 以及 filters 是 {String}

  • removeFromQueue function(value) {: Remove an item from the queue, wherevalue is {FileItem} or index of item.  從上傳隊(duì)列移除項(xiàng),value 可以是 {FileItem} 或者項(xiàng)的序號

  • clearQueue function() {: Removes all elements from the queue.  移除上傳隊(duì)列所有的元素

  • uploadItem function(value) {: Uploads an item, where value is {FileItem} or index of item.  上傳項(xiàng), value 可以是 {FileItem} 或者項(xiàng)的序號

  • cancelItem function(value) {: Cancels uploading of item, where value is{FileItem} or index of item.  取消上傳的項(xiàng)

  • uploadAll function() {: Upload all pending items on the queue.  將上傳隊(duì)列中所有的項(xiàng)進(jìn)行上傳

  • cancelAll function() {: Cancels all current uploads.  取消所有當(dāng)前上傳

  • 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.  返回項(xiàng)在上傳隊(duì)列中的序號

  • getReadyItems function() {return {Array.<FileItems>};}: Return items are ready to upload.  返回準(zhǔn)備上傳的項(xiàng)

  • getNotUploadedItems function() {return {Array.<FileItems>};}: Return an array of all pending items on the queue  返回上傳隊(duì)列中未上傳的項(xiàng)

回調(diào)函數(shù)

  • onAfterAddingFile function(item) {: 添加文件到上傳隊(duì)列后

  • onWhenAddingFileFailed function(item, filter, options) {: 添加文件到上傳隊(duì)列失敗后

  • onAfterAddingAll function(addedItems) {: 添加所選的所有文件到上傳隊(duì)列后

  • 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) {: 上傳隊(duì)列的所有文件上傳中

  • onCompleteAll function() {: 上傳隊(duì)列的所有文件上傳完成后


成功上傳文件的回調(diào)函數(shù)順序是:onAfterAddingFile — onAfterAddingAll — onBeforeUploadItem — onProgressItem — onProgressAll — onSuccessItem — onCompleteItem — onCompleteAll

FileItem

屬性

  • url {String}: Path on the server in which this file will be uploaded  上傳文件的服務(wù)器路徑

  • alias {String}: Name of the field which will contain the file, default is file  包含文件的名稱,默認(rèn)是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  與文件一起發(fā)送的表單數(shù)據(jù)

  • method {String}: It's a request method. By default POST. HTML5 browsers only.  請求方式,默認(rèn)是POST,瀏覽器需支持HTML5

  • withCredentials {Boolean} : enable CORS. HTML5 browsers only.  使用CORS,默認(rèn)是false, 瀏覽器需支持HTML5

  • removeAfterUpload {Boolean}: Remove this file from the queue after uploading  上傳之后從上傳隊(duì)列移除該文件

  • index {Number} - A sequence number upload. Read only.  上傳文件在上傳隊(duì)列中的序號,只讀

  • progress {Number}: File upload progress percentage. Read only.  文件上傳的進(jìn)度,只讀

  • isReady {Boolean} - File is ready to upload. Read only.  文件是否準(zhǔn)備好上傳,只讀

  • isUploading {Boolean}true if the file is being uploaded. Read only.  文件是否正在上傳中,只讀

  • isUploaded {Boolean}true if the file was uploaded. Read only.  文件是否已經(jīng)上傳,只讀

  • isSuccess {Boolean}true if the file was uploaded successfully. Read only.  文件是否已經(jīng)上傳成功,只讀

  • isCancel {Boolean} : true if uploading was canceled. Read only.  文件是否取消上傳,只讀

  • isError {Boolean} - true if occurred error while file uploading. Read only.  文件是否上傳錯(cuò)誤,只讀

  • uploader {Object}: Reference to the parent Uploader object for this file. Read only.  上傳該文件的Uploader ,只讀

方法

  • remove function() {: Remove this file from the queue  從上傳隊(duì)列移除該文件

  • upload function() {: Upload this file  上傳該文件

  • cancel function() {: Cancels uploading of this file  取消上傳該文件

回調(diào)函數(shù)

  • 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  上傳該文件出錯(cuò)后

  • 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;
        }
    }]
});// 另一種uploader.filters.push({
    name:'yourName2',    fn:function(item) {        returntrue;
    }
});

預(yù)定義的過濾器:

  • folder

  • queueLimit


github地址: https://github.com/nervgh/angular-file-upload/wiki/Module-API


向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI