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

Commit fe67648

Browse files
committed
Fix Blob constructor FormData transform function
1 parent 40003a4 commit fe67648

File tree

2 files changed

+23
-7
lines changed

2 files changed

+23
-7
lines changed

src/polyfill/Blob.js

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export default class Blob extends EventTarget {
2323
type:string;
2424
size:number;
2525
isRNFetchBlobPolyfill:boolean = true;
26+
multipartBoundary:string = null;
2627

2728
_ref:string = null;
2829
_blobCreated:boolean = false;
@@ -84,14 +85,15 @@ export default class Blob extends EventTarget {
8485
// process FormData
8586
else if(data instanceof FormData) {
8687
log.verbose('create Blob cache file from FormData', data)
87-
let boundary = `--RNFetchBlob-${this.cacheName}-${Date.now()}\r\n`
88+
let boundary = `RNFetchBlob-${this.cacheName}-${Date.now()}`
89+
this.multipartBoundary = boundary
8890
let parts = data.getParts()
8991
let formArray = []
9092
for(let i in parts) {
91-
formArray.push(boundary)
93+
formArray.push('\r\n--'+boundary+'\r\n')
9294
let part = parts[i]
9395
for(let j in part.headers) {
94-
formArray.push(j + part.headers[j] + ';\r\n')
96+
formArray.push(j + ': ' +part.headers[j] + ';\r\n')
9597
}
9698
formArray.push('\r\n')
9799
if(part.isRNFetchBlobPolyfill)
@@ -100,7 +102,7 @@ export default class Blob extends EventTarget {
100102
formArray.push(part.string)
101103
}
102104
log.verbose('FormData array', formArray)
103-
formArray.push(boundary + '--')
105+
formArray.push('\r\n--'+boundary+'--\r\n')
104106
p = createMixedBlobData(this._ref, formArray)
105107
}
106108
// if the data is a string starts with `RNFetchBlob-file://`, append the
@@ -125,9 +127,13 @@ export default class Blob extends EventTarget {
125127
else
126128
data = data.toString()
127129
// create cache file
130+
this.type = String(this.type).replace(/;base64/ig, '')
128131
log.verbose('create Blob cache file from string', 'encode', encoding)
129132
p = fs.writeFile(this._ref, data, encoding)
130-
.then((size) => Promise.resolve(size))
133+
.then((size) => {
134+
console.log('file bytes', size)
135+
return Promise.resolve(size)
136+
})
131137

132138
}
133139
// TODO : ArrayBuffer support
@@ -240,8 +246,9 @@ function createMixedBlobData(ref, dataArray) {
240246
let size = 0
241247
for(let i in dataArray) {
242248
let part = dataArray[i]
243-
if(part instanceof Blob)
244-
args.push([ref, part.getRNFetchBlobRef(), 'uri'])
249+
if(part.isRNFetchBlobPolyfill) {
250+
args.push([ref, part._ref, 'uri'])
251+
}
245252
else if(typeof part === 'string')
246253
args.push([ref, part, 'utf8'])
247254
// TODO : ArrayBuffer

src/polyfill/File.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,15 @@ export default class File extends Blob {
99

1010
name : string = '';
1111

12+
static build(name:string, data:any, cType):Promise<File> {
13+
this.name = name
14+
return new Promise((resolve, reject) => {
15+
new File(data, cType).onCreated((f) => {
16+
resolve(f)
17+
})
18+
})
19+
}
20+
1221
constructor(data:any , cType:string) {
1322
super(data, cType)
1423
}

0 commit comments

Comments
 (0)