HTML5 Blob Objects Made Easier
Let us simplify HTML5 blob objects for you.
Join the DZone community and get the full member experience.Join For Free
Lately, while getting ready for a HTML5 session, I found out that the File API specifications changed a little bit. In the past, in order to create a runtime Blob object you used the BlobBuilder object. That object is now deprecated and you should stop using it. Instead you are going to use the new blob object constructor which decrease the amount of code and objects you use.
Blob Object Constructor
The Blob constructor gets two parameters:
- Array of data that will be appended to the blob.
- Property bag that enable to configure the blob. You can use two properties: type and endings.
The type property indicate the content type of the Blob. The endings property gets two values: transparent and native. Those values indicate how strings containing \n are written (in native they are written to match host OS file system conventions). The transparent value is the default.
Here is an example of using the new constructor:
Running the code will produce an alert with the hello string.
The HTML5 specifications change from time to time and those changes can break your code. In this post, I wrote about a positive change in the specification regarding the use of blobs.
Published at DZone with permission of Gil Fink, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.