The Blob in JS

Source: Internet
Author: User

What is a blob?

What is a Blob? Here is the type of object that is JavaScript.

There are similar types of fields in Oracle.

In

[JS Advanced] HTML5 file operation

The file object is used in this article, and the file object is actually a more specific version of the Blob object, the BLOB stores a large amount of binary data, and the Blob's size and type attributes are inherited by the file object.

Therefore, in most cases, BLOB objects and file objects can be used in the same place, for example, you can use the FileReader interface to read data from a blob, or you can use Url.createobjecturl () to create a new URL object from a blob.

How to create a blob

1. Create a Blob object using the old method.

The old method uses Blobbuilder to create a Blob instance, and uses a append () method to insert the string (or ArrayBuffer or Blob, which is used here as a string) and, once the data is inserted successfully, you can use the GetBlob () party method to set a mime.

  <script>    var builder = new Bolbbuilder ();    Builder.append ("Hello world!");    var blob = Builder.getblob ("Text/plain");  </script>

2. New method to create BLOB objects

The new method can be created directly from the constructor of the Blob ().
Constructor, accepts two parameters, the first is a data series, can be any format value, for example, any number of strings, Blobs, and Arraybuffers. The second parameter, an object containing two properties, has two properties:

Type--MIME types.

Endings--determines the data format of the Append (), which can be either "transparent" or "native" (if the data is converted) (t* words are unchanged, n is converted by the operating system; t* is the default).

  <script>    var blob = new Blob (["Hello world!"],{type: "Text/plain"});  </script>

Application of BLOBs

1. Large File Segmentation (Slice () method)

The slice () method accepts three parameters, a starting offset, an end offset, and an optional MIME type. If the MIME type is not set, the new Blob object has the same MIME type as the parent.

This method is useful when uploading large files, splitting large files into fragments and uploading them separately, since the Blob objects after the split are separate from the original.

But currently the browser implementation of this method is not unified, Firefox uses Mozslice (), Chrome is using Webkitslice (), other browsers are normal way slice ()

You can write a method that is compatible with each browser:

    function Sliceblob (blob, start, end, type) {      type = type | | blob.type;      if (blob.mozslice) {          return Blob.mozslice (Start, end, type);      } else if (blob.webkitslice) {          return Blob.webkitslice (Start, end type);      } else {          throw new Error ("This doesn ' t work!");      }    }


2. Specify the file name of the download in chrome;

For details, refer to:

Web-side JS export CSV file (using a tag)

The Blob in JS

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.