Skip to content
This repository has been archived by the owner on Mar 16, 2019. It is now read-only.

Commit

Permalink
Fix #5 Fix #6 replace atob with base-64 library.
Browse files Browse the repository at this point in the history
  • Loading branch information
wkh237 committed May 19, 2016
1 parent 1f3b204 commit b54f0f5
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 41 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
node_modules/

# Xcode
#
# gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore
Expand Down
15 changes: 11 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ If you're dealing with image or file server that requires an `Authorization` tok

See [[fetch] Does fetch with blob() marshal data across the bridge?]([fetch] Does fetch with blob() marshal data across the bridge?).

This module enables you upload/download binary data in js, see [Examples](#user-content-usage) bellow.
This module enables you upload/download binary data in js, see [Examples](#user-content-usage) bellow.

The source code is very simple, just an implementation of native HTTP request, supports both Android (uses awesome native library [AsyncHttpClient](https://github.com/AsyncHttpClient/async-http-client])) and IOS.

Expand Down Expand Up @@ -131,6 +131,15 @@ Body of the HTTP request, body can either be a BASE64 string, or an array contai

When body is a base64 string , this string will be converted into byte array in native code, and the request body will be sent as `application/octet-stream`.

#### `base64`

A helper object simply uses [base-64](https://github.com/mathiasbynens/base64) for decode and encode BASE64 data.

```js
RNFetchBlob.base64.encode(data)
RNFetchBlob.base64.decode(data)
```

### FetchBlobResponse

When `fetch` success, it resolve a `FetchBlobResponse` object as first argument. `FetchBlobResponse` object has the following methods (these method are synchronous, so you might take quite a performance impact if the file is big)
Expand All @@ -141,11 +150,9 @@ When `fetch` success, it resolve a `FetchBlobResponse` object as first argument.
returns json parsed object (done in js context)
#### text():string
returns decoded base64 string (done in js context)
#### blob():Blob
returns Blob object (one in js context)


### TODO

* Save file to storage
* Custom MIME type in form data

43 changes: 7 additions & 36 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
* @version 0.3.3
*/

import { NativeModules } from 'react-native';
import { NativeModules } from 'react-native'
import base64 from 'base-64'

const RNFetchBlob = NativeModules.RNFetchBlob

Expand Down Expand Up @@ -51,21 +52,22 @@ class FetchBlobResponse {
* @return {blob} Return Blob object.
*/
this.blob = (contentType, sliceSize) => {
return b64toBlob(this.data, contentType, sliceSize)
console.warn('FetchBlobResponse.blob() is deprecated and has no funtionality.')
return null
}
/**
* Convert result to text.
* @return {string} Decoded base64 string.
*/
this.text = () => {
return atob(this.data)
return base64.decode(this.data)
}
/**
* Convert result to JSON object.
* @return {object} Parsed javascript object.
*/
this.json = () => {
return JSON.parse(atob(this.data))
return JSON.parse(base64.decode(this.data))
}
/**
* Return BASE64 string directly.
Expand All @@ -79,37 +81,6 @@ class FetchBlobResponse {

}

/**
* Convert base64 string to blob, source {@link http://stackoverflow.com/questions/16245767/creating-a-blob-from-a-base64-string-in-javascript}
* @param {string} b64Data Base64 string of data.
* @param {string} contentType MIME type of data.
* @param {number} sliceSize Slice size, default to 512.
* @return {blob} Return Blob object.
*/
function b64toBlob(b64Data, contentType, sliceSize) {
contentType = contentType || '';
sliceSize = sliceSize || 512;

var byteCharacters = atob(b64Data);
var byteArrays = [];

for (var offset = 0; offset < byteCharacters.length; offset += sliceSize) {
var slice = byteCharacters.slice(offset, offset + sliceSize);

var byteNumbers = new Array(slice.length);
for (var i = 0; i < slice.length; i++) {
byteNumbers[i] = slice.charCodeAt(i);
}

var byteArray = new Uint8Array(byteNumbers);

byteArrays.push(byteArray);
}

var blob = new Blob(byteArrays, {type: contentType});
return blob;
}

export default {
fetch, FetchBlobResponse
fetch, FetchBlobResponse, base64
}
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
{
"name": "react-native-fetch-blob",
"version": "0.3.3",
"version": "0.4.0",
"description": "A react-native plugin for fetch blob data via HTTP.",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"dependencies": {
"base-64": "0.1.0"
},
"keywords": [
"react-native",
"fetch",
Expand Down

0 comments on commit b54f0f5

Please sign in to comment.