mirror of
https://github.com/status-im/react-native-fs.git
synced 2025-02-28 23:10:29 +00:00
Added downloadFile method
This commit is contained in:
parent
409a9ce79c
commit
d83d0f531e
@ -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,
|
||||
|
@ -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)
|
||||
{
|
||||
|
@ -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?
|
||||
|
Loading…
x
Reference in New Issue
Block a user