У меня есть следующий код для Phonegap для загрузки изображения в PHP, который работает, но когда он будет завершен, я хочу перенаправить на index.html, хотя управление, похоже, никогда не возвращается туда, где выполняется ft.upload () - что делать Мне нужно сделать для перенаправления, когда загрузка будет завершена ??
<script type = "text/javascript" charset = "utf-8">
$(document).ready(function(){
$("#post_id").val("8");
var ls_token = get_token("token");
alert( ls_token );
$("#token_id").val( ls_token );
//
// Wait for PhoneGap to load
//
document.addEventListener("deviceready", onDeviceReady, false);
// PhoneGap is ready
//
function onDeviceReady() {
// Retrieve image file location from specified source
navigator.camera.getPicture(uploadPhoto,
function(message) { alert('get picture failed'); },
{
quality: 50,
destinationType: navigator.camera.DestinationType.FILE_URI,
sourceType: navigator.camera.PictureSourceType.PHOTOLIBRARY }
);
}
function uploadPhoto(imageURI) {
var options = new FileUploadOptions();
options.fileKey = "file";
//options.fileKey = "img";
options.fileName=imageURI.substr(imageURI.lastIndexOf('/')+1);
options.mimeType = "image/jpeg";
var params = new Object();
params.value1 = "test";
params.value2 = "param";
options.params = params;
//options.chunkedMode = false;
var ft = new FileTransfer();
ft.upload(imageURI, "http://example.com/add_pictures/", win, fail, options );
// similar behavior as an HTTP redirect
window.location.replace("index.html"); // <-- not executed..
}
function win(r) {
alert("Code = " + r.responseCode);
alert("Response = " + r.response);
console.info("Sent = " + r.bytesSent);
// similar behavior as an HTTP redirect
window.location.replace("index.html"); // <-- not executed..
}
function fail(error) {
alert("An error has occurred: Code = " + error.code);
alert("upload error source " + error.source);
alert("upload error target " + error.target);
}
});
</script>
Мне самому удалось это разобрать ... в конце концов. Я создал новый проект, используя "phonegap create .." - также обновил версию Cordova. В config.xml я добавил это:
<edit-config target = "NSCameraUsageDescription" file = "*-Info.plist" mode = "merge">
<string>need camera access to take pictures</string>
</edit-config>
<edit-config target = "NSPhotoLibraryUsageDescription" file = "*-Info.plist" mode = "merge">
<string>need photo library access to get pictures from there</string>
</edit-config>
<edit-config target = "NSLocationWhenInUseUsageDescription" file = "*-Info.plist" mode = "merge">
<string>need location access to find things nearby</string>
</edit-config>
<edit-config target = "NSPhotoLibraryAddUsageDescription" file = "*-Info.plist" mode = "merge">
<string>need photo library access to save pictures there</string>
</edit-config>
И в manifest.xml я добавил:
<uses-permission android:name = "android.permission.INTERNET" />
<uses-permission android:name = "android.permission.CAMERA" />
<uses-permission android:name = "android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name = "android.permission.ACCESS_NETWORK_STATE" />
Также согласно этой статье: https://cordova.apache.org/docs/en/latest/reference/cordova-plugin-file-transfer/ передача файлов устарела, после небольшого исследования люди, похоже, используют: XMLHttpRequest, поэтому я изменил код передачи на:
var fd = new FormData();
fd.append("file", file);
fd.append("id","88");
var posts = new XMLHttpRequest();
posts.onreadystatechange = function() { // listen for state changes
if (posts.readyState == 4 && posts.status == 200) { // when completed we can move away
window.location = "done.html";
}
}
posts.open("POST", 'http://example.com/test1.php', true);
posts.send(fd);
Не уверен, что именно решило проблему. Я предполагаю, что когда я пробовал что-то в исходном проекте, что-то было изменено или добавлено, что его расстроило