完成后,phonegap图片上传重定向
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了完成后,phonegap图片上传重定向相关的知识,希望对你有一定的参考价值。
我有以下代码用于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>
答案
我设法自己排序......最终。我使用“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);
不确定这些实际上解决了这个问题。我猜是因为我在原始项目中尝试了一些改变或添加的东西让人心烦意乱
以上是关于完成后,phonegap图片上传重定向的主要内容,如果未能解决你的问题,请参考以下文章
在 ios phonegap 应用程序中重定向到 GPS 设置屏幕