将图像从phonegap应用程序上载到WCF服务
我正在尝试使用phonegap文件传输API将图像从手机上传到WCF服务器。 下面是我的代码。但我无法将图像上载到服务器。请提出一些建议 Test.html将图像从phonegap应用程序上载到WCF服务,wcf,cordova,file-transfer,Wcf,Cordova,File Transfer,我正在尝试使用phonegap文件传输API将图像从手机上传到WCF服务器。 下面是我的代码。但我无法将图像上载到服务器。请提出一些建议 Test.html <body > <div data-role="view" id="uploadView" data-reload="true"> <div data-role="content"> <button onclick="getphoto();">get a Ph
<body >
<div data-role="view" id="uploadView" data-reload="true">
<div data-role="content">
<button onclick="getphoto();">get a Photo</button>
<img src="" id="myimg" style="border:1px solid #0f0;height:200px;width:200px;" />
</div>
</div>
<script>
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://myserver/MyAppService.svc/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>
</body>
MyService.svc.cs
namespace MyAppService
{
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class MyService : IMyService
{
public string SaveImage()
{
HttpPostedFile file = HttpContext.Current.Request.Files["recFile"];
if (file == null)
return null;
string targetFilePath = AppDomain.CurrentDomain.BaseDirectory + @"Images\Tree\\" + file.FileName;
file.SaveAs(targetFilePath);
return file.FileName.ToString();
}
}
}
谢谢你的帮助。。我发现了问题。。当我在WCF服务的web.config文件中设置aspNetCompatibilityEnabled=“true”时,我得到了HttpPostedFile 你应该试试这个
<configuration>
<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
</system.serviceModel>
</configuration>
谢谢。有什么错误?日志怎么说?System.NullReferenceException:对象引用未设置为对象的实例。在MyAppService.SaveImage()中,我认为我的服务存在一些问题。HttpPostedFile=HttpContext.Current.Request.Files[“recFile”];我能给你的最好的建议是,如果你有权访问Windows机器,那么就设置它,从设备代理它,然后检查从设备到服务器的数据包。在处理Cordova时,可能会有很多有趣的跨源问题。您是否独立测试了web服务?使用Google Chrome的扩展高级REST客户端进行测试
<configuration>
<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
</system.serviceModel>
</configuration>
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]