如何正确使用 Angular 5 中的 FileSaver?

2024-03-11

我正在尝试使用文件上传功能将文件保存到文件系统。 由于该文件是 Angular 应用程序所必需的,而不是后端(rest api - java)所必需的,所以我决定将其保存在前端应用程序中,这意味着内部的某处 角度应用程序中的资产文件夹。

我已经安装了角度文件保护程序。

模板代码:

<input type="file" name="file-7[]" id="file-7" class="inputfile inputfile-6" (change)="handleFileInput($event.target.files)">

组件代码:

import { FileSaver }  from 'angular-file-saver';

handleFileInput(files: FileList) {
    this.imageUpload = files.item(0);
    this.imageFileName = this.logoToUpload.name;
    // how to use FileSaver here ? 
    //this.imageUpload doesn't have data or something like this 

  }

知道如何将文件实际保存在文件夹中吗?谢谢。


通过安装这些包来使用 Angular Start 触发浏览器 saveAs

npm install file-saver @types/file-saver ngx-filesaver --save

将FileSaverModule模块添加到您的项目中:

import { FileSaverModule } from 'ngx-filesaver';
@NgModule({
  imports: [ FileSaverModule ]
})

然后在你的组件/服务中

//if you have issues with this line you might need to stop ng serve and relaunch it
import { FileSaverService } from 'ngx-filesaver'; 

browserSaveAs(blob: any, filename:string) {
    try {
      this.fileSaverService.save(blob, filename);
    }
    catch (e)
    {
      console.log(e) ;
    }
}

The blob可以是文字内容、图片等。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何正确使用 Angular 5 中的 FileSaver? 的相关文章

随机推荐