Skip to content

Commit df0dc1c

Browse files
committed
Use jQuery for filePost
1 parent 3b6d0c3 commit df0dc1c

File tree

1 file changed

+29
-29
lines changed

1 file changed

+29
-29
lines changed

lib/util/web_view_global_controller.dart

+29-29
Original file line numberDiff line numberDiff line change
@@ -60,58 +60,58 @@ class WebViewGlobalController {
6060
String jsonData = jsonEncode(data);
6161

6262
String jsCode = """
63-
new Promise((resolve, reject) => {
63+
new Promise((resolve, reject) => {
6464
try {
6565
var formData = new FormData();
6666
var parsedData = JSON.parse('$jsonData');
67-
67+
68+
// File processing remains the same
6869
for (var key in parsedData) {
6970
var value = parsedData[key];
70-
71-
// Decode Base64 string to binary data
7271
var binaryString = atob(value.base64);
7372
var binaryLength = binaryString.length;
7473
var binaryArray = new Uint8Array(binaryLength);
75-
74+
7675
for (var i = 0; i < binaryLength; i++) {
7776
binaryArray[i] = binaryString.charCodeAt(i);
7877
}
79-
80-
// Create Blob and File from the decoded binary data
78+
8179
var blob = new Blob([binaryArray], { type: value.mimeType });
8280
var file = new File([blob], value.filename, { type: value.mimeType });
83-
84-
// Append file to FormData
8581
formData.append('files[]', file);
8682
}
87-
88-
// Make AJAX POST request using fetch
89-
fetch('$url', {
83+
84+
// jQuery AJAX implementation
85+
\$.ajax({
86+
url: '$url',
9087
method: 'POST',
91-
body: formData,
92-
headers: JSON.parse('$jsonHeaders')
93-
})
94-
.then(response => {
95-
if (!response.ok) {
96-
throw new Error(`HTTP error! status: \${response.status}`);
97-
}
98-
return response.json(); // Assuming the server responds with JSON
99-
})
100-
.then(data => {
101-
const jsonString = JSON.stringify(data);
88+
data: formData,
89+
processData: false,
90+
contentType: false,
91+
headers: JSON.parse('$jsonHeaders'),
92+
success: function(data) {
10293
window.flutter_inappwebview.callHandler('onAjaxSuccess', data);
10394
resolve(data);
104-
})
105-
.catch(error => {
106-
window.flutter_inappwebview.callHandler('onAjaxError', { status: error.status || 'unknown', error: error.message });
107-
reject({ status: error.status || 'unknown', error: error.message });
108-
});
95+
},
96+
error: function(xhr) {
97+
var error = {
98+
status: xhr.status || 'unknown',
99+
error: xhr.responseText || xhr.statusText
100+
};
101+
window.flutter_inappwebview.callHandler('onAjaxError', error);
102+
reject(error);
103+
}
104+
});
109105
} catch (e) {
110106
console.error('Error in AJAX request:', e);
107+
window.flutter_inappwebview.callHandler('onAjaxError', {
108+
status: 'exception',
109+
error: e.message
110+
});
111111
reject(e);
112112
}
113113
});
114-
""";
114+
""";
115115

116116
try {
117117
await value?.evaluateJavascript(source: jsCode);

0 commit comments

Comments
 (0)