如何在android phonegap中将图像上传到服务器
我尝试使用phonegap api中的FileTransfer.upload上载图像。但我收到了错误。错误为“连接错误”,错误代码为3如何在android phonegap中将图像上传到服务器,android,cordova,file-transfer,Android,Cordova,File Transfer,我尝试使用phonegap api中的FileTransfer.upload上载图像。但我收到了错误。错误为“连接错误”,错误代码为3 Here is my code. <!DOCTYPE html> <html> <head> <title>Video Uploader</title> <meta name="viewport" content="width=device-width, initial-scale=1">
Here is my code.
<!DOCTYPE html>
<html>
<head>
<title>Video Uploader</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="master.css" type="text/css" />
<script type="text/javascript" charset="utf-8"src="phonegap-1.1.0.js"></script>
<script src="Scripts/jQuery-1.6.1.min.js" type="text/javascript"></script>
<script src="Scripts/jquery-1.4.1.js" type="text/javascript"></script>
<script src="Scripts/jquery-1.4.1-vsdoc.js" type="text/javascript"></script>
<script type="text/javascript" charset="utf-8">
function getphoto(){
navigator.camera.getPicture(uploadPhoto,function(message) {
alert('get picture failed');
},{ quality:10,destinationType:navigator.camera.DestinationType.FILE_URI,sourceType:navigator.camera.PictureSourceType.PHOTOLIBRARY });
}
function uploadPhoto(imageURI) {
document.getElementById("myimg").src=imageURI;
var options = new FileUploadOptions();
options.chunkedMode = false;
options.fileKey="recFile";
var imagefilename = imageURI;
options.fileName=imagefilename;
options.mimeType="image/jpeg";
var ft = new FileTransfer();
alert(imagefilename);
ft.upload(imageURI, "http://xxxx.asmx", win, fail, options,true);
}
function win(r) {
alert("Response = " + r.response);
alert("Sent = " + r.bytesSent);
}
function fail(error) {
switch (error.code) {
case FileTransferError.FILE_NOT_FOUND_ERR:
alert("Photo file not found");
break;
case FileTransferError.INVALID_URL_ERR:
alert("Bad Photo URL");
break;
case FileTransferError.CONNECTION_ERR:
alert("Connection error");
break;
}
alert("An error has occurred: Code = " + error.code);
}
</script>
</head>
<body >
<button onclick="getphoto();">get a Photo</button>
<button onclick="getphoto();">Upload a Photo</button>
<img src="" id="myimg" style="border:1px solid #0f0;height:200px;width:200px;" />
</body>
</html>
这是我的代码。
视频上传器
函数getphoto(){
navigator.camera.getPicture(上传照片,函数(消息){
警报(“获取图片失败”);
},{quality:10,destinationType:navigator.camera.destinationType.FILE_URI,sourceType:navigator.camera.PictureSourceType.PHOTOLIBRARY});
}
函数上载照片(imageURI){
document.getElementById(“myimg”).src=imageURI;
var options=new FileUploadOptions();
options.chunkedMode=false;
options.fileKey=“recFile”;
var-imagefilename=imageURI;
options.fileName=imagefilename;
options.mimeType=“image/jpeg”;
var ft=新文件传输();
警报(图像文件名);
ft.upload(imageURI,“http://xxxx.asmx“、成功、失败、选项、正确);
}
函数win(r){
警报(“响应=”+r.Response);
警报(“发送=”+r.bytesSent);
}
函数失败(错误){
开关(错误代码){
案例FileTransferError.FILE\u未找到\u错误:
警报(“未找到照片文件”);
打破
案例文件传输错误。无效\u URL\u错误:
警报(“坏照片URL”);
打破
案例文件传输错误。连接错误:
警报(“连接错误”);
打破
}
警报(“发生错误:Code=“+error.Code”);
}
拍张照片
上传照片
请查看并告诉我解决方案。phonegap api中upload.php的概念是什么?您是否尝试将其上载到其他服务器?如果上载成功,则问题可能出在服务器设置中。我遇到了这个问题,开始查看我的nginx/passenger配置 编辑: 我通过增加nginx上的设置(默认为1MB)解决了这个问题 此外,您在“ft.upload”(true)上的最后一个参数似乎是针对https请求的,因此在本例中您应该忽略该参数