Added downloadFile method

This commit is contained in:
Chris Dell 2015-10-21 00:17:03 +01:00
parent 409a9ce79c
commit d83d0f531e
3 changed files with 66 additions and 0 deletions

View File

@ -17,6 +17,7 @@ var _readFile = Promise.promisify(RNFSManager.readFile);
var _writeFile = Promise.promisify(RNFSManager.writeFile); var _writeFile = Promise.promisify(RNFSManager.writeFile);
var _unlink = Promise.promisify(RNFSManager.unlink); var _unlink = Promise.promisify(RNFSManager.unlink);
var _mkdir = Promise.promisify(RNFSManager.mkdir); var _mkdir = Promise.promisify(RNFSManager.mkdir);
var _downloadFile = Promise.promisify(RNFSManager.downloadFile);
var _pathForBundle = Promise.promisify(RNFSManager.pathForBundle); var _pathForBundle = Promise.promisify(RNFSManager.pathForBundle);
var convertError = (err) => { var convertError = (err) => {
@ -94,6 +95,11 @@ var RNFS = {
.catch(convertError); .catch(convertError);
}, },
downloadFile(url, filepath) {
return _downloadFile(url, filepath)
.catch(convertError);
},
MainBundle: RNFSManager.MainBundleDirectory, MainBundle: RNFSManager.MainBundleDirectory,
CachesDirectory: RNFSManager.NSCachesDirectory, CachesDirectory: RNFSManager.NSCachesDirectory,
DocumentDirectory: RNFSManager.NSDocumentDirectory, DocumentDirectory: RNFSManager.NSDocumentDirectory,

View File

@ -142,6 +142,22 @@ RCT_EXPORT_METHOD(readFile:(NSString *)filepath
callback(@[[NSNull null], base64Content]); 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 RCT_EXPORT_METHOD(pathForBundle:(NSString *)bundleNamed
callback:(RCTResponseSenderBlock)callback) callback:(RCTResponseSenderBlock)callback)
{ {

View File

@ -11,6 +11,11 @@ import android.content.Context;
import java.io.File; import java.io.File;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.FileInputStream; 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.NativeModule;
import com.facebook.react.bridge.ReactApplicationContext; 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 @ReactMethod
public void pathForBundle(String bundleNamed, Callback callback) { public void pathForBundle(String bundleNamed, Callback callback) {
// TODO: Not sure what equilivent would be? // TODO: Not sure what equilivent would be?