angular1 打開文件 並另存為(文件的讀取與寫入)


最近有個需求,在頁面上有個按鈕可以選取文件然后在導出到其它地方,

說明白點就是文件的讀取與寫入,下面是例子(例子中用到了fileSave.js github地址:https://github.com/eligrey/FileSaver.js)

首先引入fileSave.js   

import '~/lib/file-saver/dist/FileSaver.min.js';

html:

<input type="file" id="fileUpload" value="選擇文件" mce_style="display:none" onchange="angular.element(this).scope().fileChoose(this)" >
<button ng-click="chaunshu()" class="btn btn-default">轉移</button>

 

script:

$scope.fileChoose = (ele) => {
$scope.files = ele.files[0];//讀取文件信息
};
$scope.chaunshu = () => {
if($scope.files == '' || $scope.files == undefined ){
layer.alert('請先選擇文件', {closeBtn: false, icon: 2}, index => {
layer.close(index);
});

}else {
let filename = $scope.files.name;
let type = $scope.files.type;
let blob = new Blob([$scope.files], {type: type}); //[$scope.files]要保存的文件 {type:保存文件類型}
saveAs(blob, filename);//保存
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM