失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > angular1 打开文件 并另存为(文件的读取与写入)

angular1 打开文件 并另存为(文件的读取与写入)

时间:2023-11-17 12:00:31

相关推荐

angular1 打开文件 并另存为(文件的读取与写入)

最近有个需求,在页面上有个按钮可以选取文件然后在导出到其它地方,

说明白点就是文件的读取与写入,下面是例子(例子中用到了fileSave.js github地址:/eligrey/FileSaver.js)

首先引入fileSave.js

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

html:

<input type="file" id="fileUpload" value="选择文件" mce_style="display:none" οnchange="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);//保存

}

如果觉得《angular1 打开文件 并另存为(文件的读取与写入)》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。