HTML5 blob to convert between Arraybuffer, Typearray, and string strings

Source: Internet
Author: User

1. Converting a string string into a Blob object

// Convert a string into a Blob object var New Blob (["Hello world!" ], {    ' Text/plain '}); Console.info (BLOB); Console.info (Blob.slice (1, 3, ' Text/plain '));

2. Convert Typearray to Blob object

//convert Typearray to Blob objectvarArray =NewUint16array ([97, 32, 72, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100, 33]);//Test Success//var blob = new blob ([array], {type: "Application/octet-binary"});//Test SuccessvarBlob =NewBlob ([array]);//to read a Blob object as a stringvarReader =NewFileReader (); Reader.readastext (BLOB,' Utf-8 '); Reader.onload=function(e) {console.info (reader.result);//a Hello world!}

3, Convert Blob object to string string, use FileReader's Readastext method

// Convert a string into a Blob object var New Blob ([' Chinese string '], {    ' Text/plain '}); // Convert a Blob object to a string var New  ' utf-8 'function  (e) {    console.info (reader.result);}

4. Convert BLOB objects to Arraybuffer, using FileReader's Readasarraybuffer method

//Convert a string into a Blob objectvarBlob =NewBlob ([' Chinese string '], {type:' Text/plain '});//convert BLOB objects to ArrayBuffervarReader =NewFileReader (); Reader.readasarraybuffer (BLOB); Reader.onload=function(e) {console.info (reader.result);//ArrayBuffer {}    //frequently encountered exceptions uncaught rangeerror:byte length of Int16array should be a multiple of 2    //var buf = new Int16array (reader.result);    //Console.info (BUF);    //convert Arraybufferview to blob    varBUF =NewUint8array (Reader.result); Console.info (BUF); //[228, 184, 173, he, 135 , 229, 173, 151, 231, 172, 166, 228, 184, 178]Reader.readastext (NewBlob ([buf]), ' Utf-8 '); Reader.onload=function() {console.info (reader.result);//Chinese string    }; //convert Arraybufferview to blob    varBUF =NewDataView (Reader.result); Console.info (BUF); //DataView {}Reader.readastext (NewBlob ([buf]), ' Utf-8 '); Reader.onload=function() {console.info (reader.result);//Chinese string    };}

For Blob objects, refer to: http://www.cnblogs.com/tianma3798/p/4293660.html

HTML5 blob to convert between Arraybuffer, Typearray, and string strings

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.