6

I'm trying to save file to file system using file upload functionality. Since the file is require for the angular app and not for the backend (rest api - java), I decided to save it in the frontend app which means somewhere inside the assets folder in angular app.

I've install angular-file-saver.

Template code:

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

component code:

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 

  }

Any idea how to actually save the file in a folder ? thanks.

3 Answers 3

5

To trigger the broswer saveAs using angular start by installing these packages

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

Add the FileSaverModule module to your project:

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

Then in your component/service

//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 can be text content, image, etc.

Sign up to request clarification or add additional context in comments.

Comments

4

The simple syntax is:

  vm.download = function(text) {
    var data = new Blob([text], { type: 'text/plain;charset=utf-8' });
    FileSaver.saveAs(data, 'text.txt');
  };
}

notice the FileSaver.saveAs() method, which accepts Blob as parameter.

In your case it might look like:

handleFileInput(files: FileList) {
    this.imageUpload = files.item(0);
    this.imageFileName = this.logoToUpload.name;
    // how to use FileSaver here ? 
    var data = new Blob([imageUpload], { //your file type here });
    FileSaver.saveAs(data, this.imageFileName);  
  }

Note, you might as well need to convert the image to 64 bit and back again from 64 bit in order to save and display it

full example can be found at anguler-file-saver npm page.

20 Comments

The page is not loading when using the code: FileSaver.saveAs(data, this.imageFileName) I've installed FileSaver using "npm install angular-file-saver" but something is wrong here
Looks like that's an AngularJS library, not Angular
So, should I uninstall it and install something else ?
Yep,there's a package called ngx-filesaver that should work for Angular npmjs.com/package/ngx-filesaver
I need implement POST request then
|
4

for encoding in a different language create blob like this:

var blob = new Blob(["\ufeff"+csvArray], { type: 'text/csv;charset=utf-8' })

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.