Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix ENOENT (No such file or directory)" #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 33 additions & 6 deletions android/src/main/java/com/yoloci/fileupload/FileUploadModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,27 @@

import org.json.JSONObject;

//
import android.provider.MediaStore;
import android.provider.MediaStore.Images.Media;
import android.net.Uri;
import android.content.Context;
import android.database.Cursor;
import java.net.URL;
import java.io.File;

public class FileUploadModule extends ReactContextBaseJavaModule {

private ReactApplicationContext reactContext;

@Override
public String getName() {
return "FileUpload";
}

public FileUploadModule(ReactApplicationContext reactContext) {
super(reactContext);
this.reactContext = reactContext;
}

@ReactMethod
Expand All @@ -52,8 +64,6 @@ public void upload(final ReadableMap options, final Callback callback) {
ReadableArray files = options.getArray("files");
ReadableMap fields = options.getMap("fields");



HttpURLConnection connection = null;
DataOutputStream outputStream = null;
DataInputStream inputStream = null;
Expand All @@ -68,7 +78,6 @@ public void upload(final ReadableMap options, final Callback callback) {

connectURL = new URL(uploadUrl);


connection = (HttpURLConnection) connectURL.openConnection();

// Allow Inputs & Outputs.
Expand All @@ -85,8 +94,6 @@ public void upload(final ReadableMap options, final Callback callback) {
connection.setRequestProperty(key, headers.getString(key));
}



connection.setRequestProperty("Connection", "Keep-Alive");
connection.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary);

Expand All @@ -108,7 +115,9 @@ public void upload(final ReadableMap options, final Callback callback) {

ReadableMap file = files.getMap(i);
String filename = file.getString("filename");
String filepath = file.getString("filepath");
// transform filepath to "realPathFromURI"
String filepath = getRealPathFromURI(this.reactContext, file.getString("filepath"));

String name = file.getString("name");
filepath = filepath.replace("file://", "");
fileInputStream = new FileInputStream(filepath);
Expand Down Expand Up @@ -174,4 +183,22 @@ public void upload(final ReadableMap options, final Callback callback) {
callback.invoke("Error happened: " + ex.getMessage(), null);
}
}

// Need this to transform gallery uri to "realPath" from android Filesystem
public String getRealPathFromURI(ReactApplicationContext context, String filePath) {
Cursor cursor = null;
Uri contentUri = Uri.parse(filePath);

try {
String[] proj = { MediaStore.Images.Media.DATA };
cursor = context.getContentResolver().query(contentUri, proj, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
} finally {
if (cursor != null) {
cursor.close();
}
}
}
}