Skip to content

Commit 05edb74

Browse files
committed
fix: header in asynctask upload in android
1 parent 124857e commit 05edb74

File tree

2 files changed

+29
-12
lines changed

2 files changed

+29
-12
lines changed

android/src/main/java/com/reactnativecompressor/Utils/FileUplaoder/FileUploadHelper.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ public static FileUploadHelper fromMap(ReadableMap map) {
2020
options.method = map.getString(key);
2121
break;
2222

23+
case "headers":
24+
options.headers = map.getString(key);
25+
break;
26+
2327
case "url":
2428
options.url = map.getString(key);
2529
break;

android/src/main/java/com/reactnativecompressor/Utils/FileUplaoder/FileUploader.java

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,13 @@
1010
import com.facebook.react.bridge.ReactApplicationContext;
1111
import com.facebook.react.bridge.ReactContext;
1212
import com.facebook.react.bridge.ReadableMap;
13+
import com.facebook.react.bridge.ReadableMapKeySetIterator;
1314
import com.facebook.react.bridge.WritableMap;
1415
import com.facebook.react.modules.core.DeviceEventManagerModule;
15-
import com.reactnativecompressor.Utils.FileUplaoder.FileUploadHelper;
1616

1717
import java.io.DataOutputStream;
1818
import java.io.File;
1919
import java.io.FileInputStream;
20-
import java.io.InputStream;
21-
import java.lang.reflect.Method;
2220
import java.net.HttpURLConnection;
2321
import java.net.URL;
2422

@@ -27,6 +25,7 @@ public class FileUploader extends AsyncTask<String, Void, String> {
2725
private String fileUrl;
2826
private final FileUploadHelper options;
2927
private final ReactApplicationContext reactContext;
28+
private final String TAG="asyncTaskFileUploader";
3029
public FileUploader(String fileUrl, ReadableMap options,ReactApplicationContext reactContext,Promise promise){
3130
this.fileUrl=fileUrl;
3231
this.options= FileUploadHelper.fromMap(options);
@@ -49,12 +48,11 @@ private void sendProgressEvent(float progress) {
4948
_data.putDouble("written", progress*100);
5049
_data.putDouble("total", 100);
5150
_params.putMap("data", _data);
52-
sendEvent(reactContext, "VideoCompressorProgress", _params);
51+
sendEvent(this.reactContext, "VideoCompressorProgress", _params);
5352
}
5453

5554
@Override
5655
protected String doInBackground(String... params) {
57-
5856
try {
5957
String sourceFileUri = fileUrl;
6058

@@ -77,19 +75,25 @@ protected String doInBackground(String... params) {
7775
FileInputStream fileInputStream = new FileInputStream(
7876
sourceFile);
7977
URL url = new URL(upLoadServerUri);
80-
8178
// Open a HTTP connection to the URL
8279
conn = (HttpURLConnection) url.openConnection();
83-
conn.setDoInput(true); // Allow Inputs
8480
conn.setDoOutput(true); // Allow Outputs
81+
conn.setDoInput(true); // Allow Inputs
82+
ReadableMapKeySetIterator headerIterator = this.options.headers.keySetIterator();
83+
while (headerIterator.hasNextKey()) {
84+
String key = headerIterator.nextKey();
85+
String value = this.options.headers.getString(key);
86+
Log.d(TAG, key+" value: "+value);
87+
conn.setRequestProperty(key, value);
88+
}
89+
8590
conn.setUseCaches(false); // Don't use a Cached Copy
8691
conn.setRequestMethod(this.options.method);
8792
conn.setDoOutput(true);
8893
conn.setRequestProperty("Connection", "Keep-Alive");
8994
conn.setRequestProperty("file", sourceFileUri);
90-
91-
92-
dos = new DataOutputStream(conn.getOutputStream());
95+
conn.connect();
96+
dos = new DataOutputStream(conn.getOutputStream());
9397

9498
dos.writeBytes(twoHyphens + boundary + lineEnd);
9599

@@ -128,9 +132,14 @@ protected String doInBackground(String... params) {
128132
.getResponseMessage();
129133

130134
if (conn.getResponseCode() == 200) {
131-
Log.d("uploadFile", "doInBackground: upload successfully");
135+
Log.d(TAG, "doInBackground: upload successfully");
132136
sendProgressEvent(1);
133-
promise.resolve("");
137+
this.promise.resolve("");
138+
}
139+
else
140+
{
141+
Log.d(TAG, serverResponseMessage+" doInBackground: error"+conn.getResponseCode());
142+
this.promise.reject("");
134143
}
135144

136145
fileInputStream.close();
@@ -142,6 +151,10 @@ protected String doInBackground(String... params) {
142151
e.printStackTrace();
143152

144153
}
154+
finally {
155+
if (conn != null)
156+
conn.disconnect();
157+
}
145158
}
146159

147160

0 commit comments

Comments
 (0)