将图片从phonegap应用程序上传到WCF服务 [英] Upload image from phonegap app to WCF service
本文介绍了将图片从phonegap应用程序上传到WCF服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想使用phonegap文件传输API从我的手机上传图像到WCF服务器。
下面是我的代码。但我无法将图像上传到服务器。请提供一些建议。
Test.html
>
< div data-role =viewid =uploadViewdata-reload =true>
< div data-role =content>
< button onclick =getphoto();>获取照片< / button>
< img src =id =myimgstyle =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);
}
函数失败(错误){
switch(error.code){
case FileTransferError.FILE_NOT_FOUND_ERR:
alert(照片文件未找到);
break;
case FileTransferError.INVALID_URL_ERR:
alert(Bad Photo URL);
break;
case FileTransferError.CONNECTION_ERR:
alert(Connection error);
break;
}
alert(发生错误:Code =+ error.code);
}
< / script>
< / body>
这是我的WCF服务。
IMyService.cs
命名空间MyAppService
{
[ServiceContract]
public interface IMyService
{
[OperationContract]
[WebInvoke(BodyStyle = WebMessageBodyStyle.Wrapped,Method =POST,RequestFormat = WebMessageFormat.Json,ResponseFormat = WebMessageFormat.Json,UriTemplate =SaveImage)]
string SaveImage();
}
}
MyService.svc.cs
命名空间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>
或
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
I am trying to upload image from my mobile to WCF server using phonegap file transfer API. Following is my code. But I am not able to upload the image to server. Please give some suggestion.
Test.html
<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>
Here is my WCF service.
IMyService.cs
namespace MyAppService
{
[ServiceContract]
public interface IMyService
{
[OperationContract]
[WebInvoke(BodyStyle = WebMessageBodyStyle.Wrapped, Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, UriTemplate = "SaveImage")]
string SaveImage();
}
}
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();
}
}
}
Thanks
解决方案
Thanks for the help.. I found the issue.. When I set aspNetCompatibilityEnabled="true" in web.config file of WCF service, I got the HttpPostedFile.
should try this
<configuration>
<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
</system.serviceModel>
</configuration>
or
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
Thanks..
这篇关于将图片从phonegap应用程序上传到WCF服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文