wcf - 将图片从 phonegap 应用程序上传到 WCF 服务

标签 wcf cordova file-transfer

我正在尝试使用 phonegap 文件传输 API 将图像从我的手机上传到 WCF 服务器。 以下是我的代码。但是我无法将图像上传到服务器。请提出一些建议。

测试.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>

这是我的 WCF 服务。

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();

    }
}

我的服务.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>

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]

谢谢..

关于wcf - 将图片从 phonegap 应用程序上传到 WCF 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17232183/

相关文章:

python - 扭曲不发送整个文件与扭曲的客户端和服务器 tcp 传输

c# - 添加服务引用给出异常 : Unable to connect to remote server

javascript - jQuery 移动 : Is there a way to do some js before the first page shows up?

linux - 路由器文件传输

android - 手机在 Debug模式下无法连接到 Eclipse

javascript - 如何使用 PhoneGap 保存网页供离线使用?

c# - DirectoryCatalog 和 AssemblyCatalog 之间的区别

WCF IsOneWay = true 报告异常

c# - WCF ServiceHost.Close() 延迟

jquery - 在 Windows Phone 上通过 PhoneGap 使用 jQuery .find()