从Android的PhoneGap的图像上传到服务器使用ASMX [英] Upload image from android phonegap to a server using asmx

查看:310
本文介绍了从Android的PhoneGap的图像上传到服务器使用ASMX的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从PhoneGap的应用(Android版)载捕捉/库的图像,当我打电话ASMX Web服务我得到一个连接错误,请注意,移动和服务器在同一个网络上

这是我的code:

 <!DOCTYPE HTML PUBLIC -  // W3C // DTD HTML 4.01 // ENhttp://www.w3.org/TR/html4/strict.dtd >
< HTML和GT;
< HEAD>
    <标题>文件传输示例< /标题>    <脚本类型=文/ JavaScript的字符集=utf-8SRC =PhoneGap的-1.3.0.js>< / SCRIPT>
      <脚本类型=文/ JavaScript的字符集=utf-8SRC =jquery.js和>< / SCRIPT>
    <脚本类型=文/ JavaScript的字符集=utf-8>
功能getphoto(){
       navigator.camera.getPicture(uploadPhoto,功能(消息){
       警报('获取图片失败');
       },{质量: 10,destinationType:navigator.camera.DestinationType.FILE_URI,sourceType:navigator.camera.PictureSourceType.PHOTOLIBRARY });
       }功能uploadPhoto(imageURI){的document.getElementById(myimg)SRC = imageURI。            VAR的选择=新FileUploadOptions();
            options.chunkedMode = FALSE;
            options.fileKey =recFile;
            VAR映像文件名称= imageURI;
            options.fileName =映像文件名称;
            options.mimeType =图像/ JPEG;
           // VAR PARAMS =新的对象();
            //params.value1 =测试;
            //params.value2 =参数;
            //options.params =参数;            VAR英尺=新的文件传输();
            警报(映像文件名称);
            //警报(选件);
            //警报(PARAMS);
            ft.upload(imageURI,http://10.3.150.16/WebSite1/Service.asmx/SaveImage,赢,失败,期权);
        }
功能赢(R){
            //console.log(\"$c$c =+ r.response code);
            //console.log(\"Response =+ r.response);
            警报(已发送=+ r.bytesSent);
        }
        功能失效(错误){
         开关(误差。code){
                    案例FileTransferError.FILE_NOT_FOUND_ERR:
                        警报(照片找不到文件);
                        打破;
                    案例FileTransferError.INVALID_URL_ERR:
                        警报(坏照片的URL);
                        打破;
                    案例FileTransferError.CONNECTION_ERR:
                        警报(连接错误);
                        打破;
                }            (发生错误:code =+错误code)警报;
        }
        < / SCRIPT>
< /头>
<身体GT;
    <按钮的onclick =getphoto();>获得照片< /按钮>
    <按钮的onclick =getphoto();>上传一张照片< /按钮>
    < IMG SRC =ID =myimg的风格=边界:1px的固体#0f0;高度:200像素;宽度:200像素; />
< /身体GT;
< / HTML>

和这里是我的asmx我们服务code:

 使用系统;
使用System.Collections.Generic;
使用System.Linq的;
使用的System.Web;
使用System.Web.Services;[WebService的空间(namespace =htt​​p://tempuri.org/)]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)
//要允许此Web服务从脚本调用,使用ASP.NET AJAX,取消注释以下行。
// [System.Web.Script.Services.ScriptService]公共类服务:System.Web.Services.WebService
{
    [的WebMethod]
    公共字符串SaveImage()
    {
        HttpPostedFile文件= HttpContext.Current.Request.Files [recFile];
        如果(文件== NULL)
            返回null;
        字符串targetFilePath =C:\\\\ \\\\存款+ file.FileName;
        file.SaveAs(targetFilePath);
        返回file.FileName.ToString();
    }
}


解决方案

使用的code path.GetFileName(file.FileName),那就是:

 字符串targetFilePath =C:\\\\ \\\\存款+ Path.GetFileName(file.FileName);

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

解决方案

Use the code path.GetFileName(file.FileName), that is:

string targetFilePath = "c:\\deposit\\" + Path.GetFileName(file.FileName);

这篇关于从Android的PhoneGap的图像上传到服务器使用ASMX的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆