diff --git a/FS.common.js b/FS.common.js index c553a57..35de1fa 100644 --- a/FS.common.js +++ b/FS.common.js @@ -17,6 +17,7 @@ var _readFile = Promise.promisify(RNFSManager.readFile); var _writeFile = Promise.promisify(RNFSManager.writeFile); var _unlink = Promise.promisify(RNFSManager.unlink); var _mkdir = Promise.promisify(RNFSManager.mkdir); +var _downloadFile = Promise.promisify(RNFSManager.downloadFile); var _pathForBundle = Promise.promisify(RNFSManager.pathForBundle); var convertError = (err) => { @@ -94,6 +95,11 @@ var RNFS = { .catch(convertError); }, + downloadFile(url, filepath) { + return _downloadFile(url, filepath) + .catch(convertError); + }, + MainBundle: RNFSManager.MainBundleDirectory, CachesDirectory: RNFSManager.NSCachesDirectory, DocumentDirectory: RNFSManager.NSDocumentDirectory, diff --git a/RNFSManager.m b/RNFSManager.m index a8caf4e..b2a156d 100644 --- a/RNFSManager.m +++ b/RNFSManager.m @@ -142,6 +142,22 @@ RCT_EXPORT_METHOD(readFile:(NSString *)filepath callback(@[[NSNull null], base64Content]); } +RCT_EXPORT_METHOD(downloadFile:(NSString *)urlStr + filepath:(NSString *)filepath + callback:(RCTResponseSenderBlock)callback) +{ + NSURL *url = [NSURL URLWithString:urlStr]; + NSData *urlData = [NSData dataWithContentsOfURL:url]; + + BOOL success = NO; + + if (urlData) { + success = [urlData writeToFile:filepath atomically:YES]; + } + + callback(@[[NSNull null], [NSNumber numberWithBool:success], filepath]); +} + RCT_EXPORT_METHOD(pathForBundle:(NSString *)bundleNamed callback:(RCTResponseSenderBlock)callback) { diff --git a/android/src/main/java/com/rnfs/RNFSManager.java b/android/src/main/java/com/rnfs/RNFSManager.java index 12ba3f3..1a5c0be 100644 --- a/android/src/main/java/com/rnfs/RNFSManager.java +++ b/android/src/main/java/com/rnfs/RNFSManager.java @@ -11,6 +11,11 @@ import android.content.Context; import java.io.File; import java.io.FileOutputStream; import java.io.FileInputStream; +import java.io.BufferedInputStream; +import java.io.InputStream; +import java.io.OutputStream; +import java.net.URL; +import java.net.URLConnection; import com.facebook.react.bridge.NativeModule; import com.facebook.react.bridge.ReactApplicationContext; @@ -173,6 +178,45 @@ public class RNFSManager extends ReactContextBaseJavaModule { } } + @ReactMethod + public void downloadFile(String urlStr, String filepath, Callback callback) { + try { + File file = new File(filepath); + + URL url = new URL(urlStr); + URLConnection connection = url.openConnection(); + + connection.connect(); + + int lengthOfFile = connection.getContentLength(); + + InputStream input = new BufferedInputStream(url.openStream(), 8192); + OutputStream output = new FileOutputStream(filepath); + + byte data[] = new byte[1024]; + long total = 0; + int count; + + while ((count = input.read(data)) != -1) { + total += count; + //int progress = (int)((total * 100) / lengthOfFile); + output.write(data, 0, count); + } + + output.flush(); + + output.close(); + input.close(); + + boolean success = true; + + callback.invoke(null, success, filepath); + } catch (Exception ex) { + ex.printStackTrace(); + callback.invoke(makeErrorPayload(ex)); + } + } + @ReactMethod public void pathForBundle(String bundleNamed, Callback callback) { // TODO: Not sure what equilivent would be?