Skip to content
Open
Show file tree
Hide file tree
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
10 changes: 7 additions & 3 deletions FileUpload.m
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,13 @@ @implementation FileUpload
NSInteger statusCode = [response statusCode];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];

NSDictionary *res=[[NSDictionary alloc] initWithObjectsAndKeys:[NSNumber numberWithInteger:statusCode],@"status",returnString,@"data",nil];

callback(@[[NSNull null], res]);
if ((statusCode >= 200) && (statusCode < 300)) {
NSDictionary *res=[[NSDictionary alloc] initWithObjectsAndKeys:[NSNumber numberWithInteger:statusCode],@"status",returnString,@"data",nil];
callback(@[[NSNull null], res]);
} else {
NSString * errorResponse = [NSString stringWithFormat:@"%ld: %@", statusCode, [NSHTTPURLResponse localizedStringForStatusCode: statusCode]];
callback(@[errorResponse, [NSNull null]]);
}
}

- (NSString *)generateBoundaryString
Expand Down
6 changes: 3 additions & 3 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ buildscript {
apply plugin: 'com.android.library'

android {
compileSdkVersion 23
buildToolsVersion "23.0.1"
compileSdkVersion 26
buildToolsVersion "26.0.2"

defaultConfig {
minSdkVersion 16
Expand All @@ -30,5 +30,5 @@ repositories {
}

dependencies {
compile 'com.facebook.react:react-native:0.15.+'
compile "com.facebook.react:react-native:+"
}
19 changes: 13 additions & 6 deletions android/src/main/java/com/yoloci/fileupload/FileUploadModule.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.yoloci.fileupload;

import android.net.Uri;
import android.os.Bundle;

import com.facebook.react.bridge.Arguments;
Expand All @@ -12,6 +13,7 @@
import com.facebook.react.bridge.Callback;

import java.io.DataInputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.io.DataOutputStream;
Expand All @@ -25,13 +27,16 @@

public class FileUploadModule extends ReactContextBaseJavaModule {

private final ReactApplicationContext _reactContext;

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

public FileUploadModule(ReactApplicationContext reactContext) {
super(reactContext);
_reactContext = reactContext;
}

@ReactMethod
Expand All @@ -58,7 +63,7 @@ public void upload(final ReadableMap options, final Callback callback) {
DataOutputStream outputStream = null;
DataInputStream inputStream = null;
URL connectURL = null;
FileInputStream fileInputStream = null;
InputStream fileInputStream = null;

int bytesRead, bytesAvailable, bufferSize;
byte[] buffer;
Expand Down Expand Up @@ -107,13 +112,15 @@ public void upload(final ReadableMap options, final Callback callback) {
for (int i = 0; i < files.size(); i++) {

ReadableMap file = files.getMap(i);
String name = file.getString("name");
String filename = file.getString("filename");
String filepath = file.getString("filepath");
filepath = filepath.replace("file://", "");
fileInputStream = new FileInputStream(filepath);
String filetype = file.getString("filetype");
fileInputStream = _reactContext.getContentResolver().openInputStream(Uri.parse(filepath));

outputStream.writeBytes(twoHyphens + boundary + lineEnd);
outputStream.writeBytes("Content-Disposition: form-data; name=\"image\";filename=\"" + filename + "\"" + lineEnd);
outputStream.writeBytes("Content-Disposition: form-data; name=\"" + name + "\";filename=\"" + filename + "\"" + lineEnd);
outputStream.writeBytes("Content-Type: " + filetype + lineEnd);
outputStream.writeBytes(lineEnd);

bytesAvailable = fileInputStream.available();
Expand All @@ -139,11 +146,11 @@ public void upload(final ReadableMap options, final Callback callback) {

int serverResponseCode = connection.getResponseCode();
String serverResponseMessage = connection.getResponseMessage();
if (serverResponseCode != 200) {
if (!((serverResponseCode >= 200) && (serverResponseCode < 300))) {
fileInputStream.close();
outputStream.flush();
outputStream.close();
callback.invoke("Error happened: " + serverResponseMessage, null);
callback.invoke(serverResponseCode + ": " + serverResponseMessage, null);
} else {
BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
StringBuilder sb = new StringBuilder();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public List<NativeModule> createNativeModules(
return modules;
}

@Override
// Deprecated from RN 0.47
public List<Class<? extends JavaScriptModule>> createJSModules() {
return Collections.emptyList();
}
Expand All @@ -39,4 +39,4 @@ public List<Class<? extends JavaScriptModule>> createJSModules() {
public List<ViewManager> createViewManagers(ReactApplicationContext reactContext) {
return Arrays.asList();
}
}
}