mirror of
https://github.com/status-im/react-native-fs.git
synced 2026-08-31 03:51:11 +00:00
Added downloadFile method
This commit is contained in:
@@ -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?
|
||||
|
||||
Reference in New Issue
Block a user