An Android application that works on the web. I need to upload a PDF file and use it on the web
Uploading a file from Android works correctly for you to the web, showing many errors
After trying a lot, this is the last code
Future<int> uploadFileWeb(String likePage,file,String filename,Map<String,String> data)async{
var request = http.MultipartRequest(
'POST', _getUtl(likePage),
);
Map<String,String>headers= _getHeaders();
headers["Content-type"]="multipart/form-data";
request.headers.addAll(headers);
if(data.isNotEmpty){
request.fields.addAll(data);
}
request.files.add(
http.MultipartFile.fromBytes(
"file", file,
contentType:MediaType('files', 'pdf')
),
);
var response = await request.send();
if(response.statusCode==200){
await response.stream.bytesToString().then((value) {
print(value);
var jsonResponse = json.decode(value);
fromJson(jsonResponse['result']);
});
return 1;
}else{
return 0;
}
}
php code
public function uploadFile()
{
$NAMFPOST = "file";
$uploadDir = 'files/'; // مجلد حفظ الملفات
$ddd = $_FILES[$NAMFPOST]["name"];
$FILE_PATH = $_FILES[$NAMFPOST]["tmp_name"]; // Full path to your local file
$uploadedFile = $uploadDir . basename($_FILES['file']['name']);
if (move_uploaded_file($FILE_PATH, $uploadedFile)) {
return 'File uploaded successfully! ' . $ddd;
} else {
return 'Error uploading file!';
}
}
error in php
Undefined array key "file"
and
Trying to access array offset on value of type null in
………………………….
error flutter
dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/errors.dart 297:3 throw_
dart-sdk/lib/_internal/js_dev_runtime/patch/convert_patch.dart 38:5 _parseJson
dart-sdk/lib/convert/json.dart 610:36 convert
dart-sdk/lib/convert/json.dart 216:41 decode
packages/yourdoctor/serves/getDataApi.dart 113:33 <fn>
dart-sdk/lib/async/zone.dart 1661:54 runUnary
dart-sdk/lib/async/future_impl.dart 162:18 handleValue
dart-sdk/lib/async/future_impl.dart 838:44 handleValueCallback
dart-sdk/lib/async/future_impl.dart 867:13 _propagateToListeners
dart-sdk/lib/async/future_impl.dart 634:7 [_complete]
dart-sdk/lib/async/stream.dart 1094:13 <fn>
dart-sdk/lib/async/zone.dart 1582:9 runGuarded
dart-sdk/lib/async/stream_impl.dart 392:7 sendDone
dart-sdk/lib/async/stream_impl.dart 402:7 [_sendDone]
dart-sdk/lib/async/stream_impl.dart 291:7 [_close]
dart-sdk/lib/async/stream_transformers.dart 87:11 [_close]
dart-sdk/lib/async/stream_transformers.dart 21:5 close
dart-sdk/lib/convert/string_conversion.dart 241:5 close
dart-sdk/lib/convert/string_conversion.dart 295:7 close
dart-sdk/lib/convert/chunked_conversion.dart 78:5 close
dart-sdk/lib/async/stream_transformers.dart 132:7 [_handleDone]
dart-sdk/lib/async/zone.dart 1582:9 runGuarded
2
Answers
Undefined array key "file" in PHP. The PHP script tries to access $_FILES[‘file’], however it appears you’re using a different key ($NAMFPOST = "file";). You should utilize $NAMFPOST rather than hardcoding ‘file’ into the $_FILES array. Update your PHP script accordingly.
here some example :
select file
and upload