Загрузить изображение из Android PhoneGap на сервер, используя asmx - PullRequest
0 голосов
/ 27 декабря 2011

Я пытаюсь загрузить захваченное / библиотечное изображение из приложения phonegap (android), когда я звоню в веб-службу asmx, я получаю сообщение об ошибке соединения, обратите внимание, что мобильный и сервер находятся в одной сети

вот мой код:

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

Ответы [ 2 ]

1 голос
/ 30 мая 2012

Используйте код path.GetFileName(file.FileName), то есть:

string targetFilePath = "c:\\deposit\\" + Path.GetFileName(file.FileName);
0 голосов
/ 02 февраля 2012

Может быть проблема с брандмауэром?Включили ли вы порт 80 для доступа к внешним устройствам?Обратите внимание, что вы должны размещать службу в IIS, а не в Kasini (служба Visual Studio по умолчанию)

Ta

Ross

...