Browse Source

Fix unicode write file issue #73

Ben Hsieh 8 years ago
parent
commit
5e5a461925

+ 6
- 6
src/android/src/main/java/com/RNFetchBlob/RNFetchBlobFS.java View File

94
                         }
94
                         }
95
                         FileInputStream fin = new FileInputStream(src);
95
                         FileInputStream fin = new FileInputStream(src);
96
                         byte [] buffer = new byte [10240];
96
                         byte [] buffer = new byte [10240];
97
-                        int read = fin.read(buffer);
98
-                        written = read;
99
-                        while(read > 0) {
97
+                        int read;
98
+                        written = 0;
99
+                        while((read = fin.read(buffer)) > 0) {
100
                             fout.write(buffer, 0, read);
100
                             fout.write(buffer, 0, read);
101
-                            read = fin.read(buffer);
102
                             written += read;
101
                             written += read;
103
                         }
102
                         }
104
                         fin.close();
103
                         fin.close();
798
             byte [] b = Base64.decode(data, Base64.NO_WRAP);
797
             byte [] b = Base64.decode(data, Base64.NO_WRAP);
799
             if(encoding.toLowerCase().contains("urlencode")) {
798
             if(encoding.toLowerCase().contains("urlencode")) {
800
                 try {
799
                 try {
801
-                    b = URLDecoder.decode(new String(b), "UTF-8").getBytes();
802
-                } catch (UnsupportedEncodingException e) {
800
+                    String encoded = URLDecoder.decode(new String(b), "UTF-8");
801
+                    b = encoded.getBytes();
802
+                } catch (Exception e) {
803
                     e.printStackTrace();
803
                     e.printStackTrace();
804
                 }
804
                 }
805
             }
805
             }

+ 5
- 6
src/android/src/main/java/com/RNFetchBlob/RNFetchBlobReq.java View File

98
     public boolean reportUploadProgress = false;
98
     public boolean reportUploadProgress = false;
99
 
99
 
100
     public RNFetchBlobReq(ReadableMap options, String taskId, String method, String url, ReadableMap headers, String body, ReadableArray arrayBody, final Callback callback) {
100
     public RNFetchBlobReq(ReadableMap options, String taskId, String method, String url, ReadableMap headers, String body, ReadableArray arrayBody, final Callback callback) {
101
-        this.method = method;
101
+        this.method = method.toUpperCase();
102
         this.options = new RNFetchBlobConfig(options);
102
         this.options = new RNFetchBlobConfig(options);
103
         this.taskId = taskId;
103
         this.taskId = taskId;
104
         this.url = url;
104
         this.url = url;
404
                         String dest = RNFetchBlobFS.getTmpPath(ctx, taskId);
404
                         String dest = RNFetchBlobFS.getTmpPath(ctx, taskId);
405
                         InputStream ins = resp.body().byteStream();
405
                         InputStream ins = resp.body().byteStream();
406
                         FileOutputStream os = new FileOutputStream(new File(dest));
406
                         FileOutputStream os = new FileOutputStream(new File(dest));
407
+                        int read;
407
                         byte [] buffer = new byte[10240];
408
                         byte [] buffer = new byte[10240];
408
-                        int read = ins.read(buffer);
409
-                        os.write(buffer,0,read);
410
-                        while(read > 0) {
411
-                            os.write(buffer,0,read);
412
-                            read = ins.read(buffer);
409
+                        while ((read = ins.read(buffer)) != -1) {
410
+                            os.write(buffer, 0, read);
413
                         }
411
                         }
414
                         ins.close();
412
                         ins.close();
413
+                        os.flush();
415
                         os.close();
414
                         os.close();
416
                         callback.invoke(null, null, dest);
415
                         callback.invoke(null, null, dest);
417
                     }
416
                     }

+ 4
- 1
src/ios/RNFetchBlobFS.m View File

229
             if([encoding containsString:@"urlencode"])
229
             if([encoding containsString:@"urlencode"])
230
             {
230
             {
231
                 NSString * decode = [[[NSString alloc] initWithData:content encoding:NSUTF8StringEncoding] stringByRemovingPercentEncoding];
231
                 NSString * decode = [[[NSString alloc] initWithData:content encoding:NSUTF8StringEncoding] stringByRemovingPercentEncoding];
232
-                content = [decode dataUsingEncoding:NSUTF8StringEncoding];
232
+                if(decode != nil)
233
+                {
234
+                    content = [decode dataUsingEncoding:NSUTF8StringEncoding];
235
+                }
233
             }
236
             }
234
         }
237
         }
235
         else if([encoding isEqualToString:@"uri"]) {
238
         else if([encoding isEqualToString:@"uri"]) {