如何将图像上传到服务器在android phonegap
问题描述:
我试图上传图像使用FileTransfer.upload在phonegap api.But我得到错误。错误是“连接错误”,错误代码是3.如何将图像上传到服务器在android phonegap
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>
,请看到告诉我的solution.What upload.php的是在PhoneGap的API
答
你试过上传到另一台服务器的概念?如果上传成功,则问题可能出现在您的服务器设置中。我有这个麻烦,并开始看我的nginx /乘客conf。
编辑:
我解决了这个由nginx的增加client_max_body_size设置(默认为1MB)。
另外,看起来“ft.upload”(“true”)上的最后一个参数是用于https请求的,因此在这种情况下应该省略。