ソースを参照

Fix Blob constructor FormData transform function

Ben Hsieh 8 年 前
コミット
fe67648845
共有2 個のファイルを変更した23 個の追加7 個の削除を含む
  1. 14
    7
      src/polyfill/Blob.js
  2. 9
    0
      src/polyfill/File.js

+ 14
- 7
src/polyfill/Blob.js ファイルの表示

@@ -23,6 +23,7 @@ export default class Blob extends EventTarget {
23 23
   type:string;
24 24
   size:number;
25 25
   isRNFetchBlobPolyfill:boolean = true;
26
+  multipartBoundary:string = null;
26 27
 
27 28
   _ref:string = null;
28 29
   _blobCreated:boolean = false;
@@ -84,14 +85,15 @@ export default class Blob extends EventTarget {
84 85
     // process FormData
85 86
     else if(data instanceof FormData) {
86 87
       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
88 90
       let parts = data.getParts()
89 91
       let formArray = []
90 92
       for(let i in parts) {
91
-        formArray.push(boundary)
93
+        formArray.push('\r\n--'+boundary+'\r\n')
92 94
         let part = parts[i]
93 95
         for(let j in part.headers) {
94
-          formArray.push(j + part.headers[j] + ';\r\n')
96
+          formArray.push(j + ': ' +part.headers[j] + ';\r\n')
95 97
         }
96 98
         formArray.push('\r\n')
97 99
         if(part.isRNFetchBlobPolyfill)
@@ -100,7 +102,7 @@ export default class Blob extends EventTarget {
100 102
           formArray.push(part.string)
101 103
       }
102 104
       log.verbose('FormData array', formArray)
103
-      formArray.push(boundary + '--')
105
+      formArray.push('\r\n--'+boundary+'--\r\n')
104 106
       p = createMixedBlobData(this._ref, formArray)
105 107
     }
106 108
     // if the data is a string starts with `RNFetchBlob-file://`, append the
@@ -125,9 +127,13 @@ export default class Blob extends EventTarget {
125 127
       else
126 128
         data = data.toString()
127 129
       // create cache file
130
+      this.type = String(this.type).replace(/;base64/ig, '')
128 131
       log.verbose('create Blob cache file from string', 'encode', encoding)
129 132
       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
+            })
131 137
 
132 138
     }
133 139
     // TODO : ArrayBuffer support
@@ -240,8 +246,9 @@ function createMixedBlobData(ref, dataArray) {
240 246
   let size = 0
241 247
   for(let i in dataArray) {
242 248
     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
+    }
245 252
     else if(typeof part === 'string')
246 253
       args.push([ref, part, 'utf8'])
247 254
     // TODO : ArrayBuffer

+ 9
- 0
src/polyfill/File.js ファイルの表示

@@ -9,6 +9,15 @@ export default class File extends Blob {
9 9
 
10 10
   name : string = '';
11 11
 
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
+
12 21
   constructor(data:any , cType:string) {
13 22
     super(data, cType)
14 23
   }