我有以下用于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.log("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>
答案 0 :(得分:0)
我设法自己整理了一下……最终。我使用“ 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);
不确定其中哪一个真正解决了问题。我猜想在尝试原始项目中的内容时,发生了一些更改或添加,从而使它不满意