TypeScript banner
| | | |

TypeScript Storing Files

Unlike my previous articles, this one will be covering a new style of articles, what I’d like to call a ‘hack-it article‘ and in this article I’ll be going over how you can persist a JavaScript file object to some HTML5 storage. Whether you use local storage or session storage, that’s up to you, the logic used is practically the same.

So, let’s begin, in order to understand entirely what’s going on here, you need to have a relatively basic understanding of modern day JavaScript, including RxJS, promises, arrow functions, TypeScript & all the other good stuff. So, let’s begin by going over why I initially created this script, it was actually work related, I wanted to mimic an existing application that uses JSF & I wanted to see how challenging it was to convert it to Angular. As it turns out, if you’re familiar with JavaScript & Primefaces, it’s incredibly easy, a breeze even. One of the features in this app includes a lot of stateful scenarios, one of those being uploading files to be stored in the session object, so, to replicate this, I tried to store the files into the session storage object.

The solution is simple, very simple, so, without further ado, let’s see some code, to keep things simple, I initially created a class called FileStore, all this class really needed in my opinion was two methods, one to simply store the files, another to simply retrieve the files, that’s it. I decided, since I was using Angular, what an appropriate time to take advantage of RxJS. So, to give you the solution, here you go;

To walk you through the code, it’s quite simple really, in order to store the file object(s) to HTML5 storage, all that’s really going on consists of converting these file objects to the custom POJO called BasicFileInformation. But in order to do this, we must take advantage of the FileReader API, by making use of the FileReader, we’re able to read the contents of the file as a data URL. But the only form of challenge here is that in order to do this, the FileReader’s method to do this is asynchronous, hence why I thought it suitable to take advantage of the observable that RxJS so kindly provides us. Once all of the files have been processed, the idea is to have your state management code elsewhere, there you can subscribe to this sotreFiles method & carry on like so. As I was using Angular, I wanted a nice way to load the files from HTML5 storage, hence the creation of the loadFiles method.

In the loadFiles method, again, very simple stuff, all that we’re doing here is essentially loading the file data by using the fetch API. Due to the fact that this is essentially an ajax request, that’s right, more asynchronous code, so, to keep things nice & consistent, I decided to return another observable object.

Conclusion

This may not be the best solution to persist files to HTML5 storage, however, it’s very simple, very lightweight & it’s incredibly easy to use. If you’re familiar with Angular, you should be able to make use/sense of this code without even having to think about it. In my personal opinion, it’s so simple that even without a great deal of knowledge about the current JavaScript ecosystem, a competent developer should be able to make sense of it to some extent or another.

Similar Posts

  • |

    Empowering The Backend

    Introduction I’ve been involved in all kinds of web-based projects throughout my career so far. I hope that pattern continues & I even hope that someday, I can even go out of my comfort zone & deal with technologies beyond the web. Be that embedded systems programming for IoT devices or just building some silly…

  • Organic vs Artificial

    Introduction Don’t worry, I won’t bore you by talking about the likes of food & how things are grown, processed, etc. Rather, I’d like to revisit the topic of artificial intelligence, and review its weak points at this moment in time. If you’ve read my previous articles, you’ll know that I’m not one of those…

  • | | |

    QOS Status Report

    I’ll keep this one relatively short. I just thought that I’d share what I’ve been up to lately, I’m currently a very, very busy bee! 🐝 One thing that I’ve recently worked on at QuoteOnSite is what we’re calling the content library, from an end user perspective, this is essentially a web based file system….

  • |

    TypeScript DI

    Since it’s been such a long time since I’ve done anything fun, technical or exciting on my personal website, I thought that it would be nice to consider looking at implementing a relatively simplistic IOC solution in TypeScript. I decided to use the more Java oriented approach, whereby I’m using property & class decorators to…

Leave a Reply

Your email address will not be published. Required fields are marked *