使用 asmx 将图像从 Android PhoneGap 上传到服务器
我正在尝试从phonegap应用程序(android)上传捕获/库图像,当调用asmx Web服务时,我收到连接错误,请注意,移动设备和服务器位于同一网络上,
这是我的代码:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>File Transfer Example</title>
<script type="text/javascript" charset="utf-8" src="phonegap-1.3.0.js"></script>
<script type="text/javascript" charset="utf-8" src="jquery.js"></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 params = new Object();
//params.value1 = "test";
//params.value2 = "param";
//options.params = params;
var ft = new FileTransfer();
alert(imagefilename);
//alert(options);
//alert(params);
ft.upload(imageURI, "http://10.3.150.16/WebSite1/Service.asmx/SaveImage", win, fail, options);
}
function win(r) {
//console.log("Code = " + r.responseCode);
//console.log("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>
这是我的asmx,我们服务代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
// [System.Web.Script.Services.ScriptService]
public class Service : System.Web.Services.WebService
{
[WebMethod]
public string SaveImage()
{
HttpPostedFile file = HttpContext.Current.Request.Files["recFile"];
if (file == null)
return null;
string targetFilePath = "c:\\deposit\\" + file.FileName;
file.SaveAs(targetFilePath);
return file.FileName.ToString();
}
}
I'm trying to upload captured/library image from phonegap application (android) , when am calling asmx web service i got a connection error , note that mobile and server are on the same network
here is my code :
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>File Transfer Example</title>
<script type="text/javascript" charset="utf-8" src="phonegap-1.3.0.js"></script>
<script type="text/javascript" charset="utf-8" src="jquery.js"></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 params = new Object();
//params.value1 = "test";
//params.value2 = "param";
//options.params = params;
var ft = new FileTransfer();
alert(imagefilename);
//alert(options);
//alert(params);
ft.upload(imageURI, "http://10.3.150.16/WebSite1/Service.asmx/SaveImage", win, fail, options);
}
function win(r) {
//console.log("Code = " + r.responseCode);
//console.log("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>
and here is my asmx we service code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
// [System.Web.Script.Services.ScriptService]
public class Service : System.Web.Services.WebService
{
[WebMethod]
public string SaveImage()
{
HttpPostedFile file = HttpContext.Current.Request.Files["recFile"];
if (file == null)
return null;
string targetFilePath = "c:\\deposit\\" + file.FileName;
file.SaveAs(targetFilePath);
return file.FileName.ToString();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用代码
path.GetFileName(file.FileName)
,即:Use the code
path.GetFileName(file.FileName)
, that is:可能是防火墙问题?您是否已启用端口 80 访问外部设备?请注意,您必须在 IIS 中托管该服务,而不是 Kasini(默认的 Visual Studio 服务)
Ta
Ross
Could be a firewall issue? Have yo enabled port 80 access to external devices? Note you must host the service in IIS not Kasini (the default visual studio service)
Ta
Ross