现在的位置: 首页 > 综合 > 正文

JQuery调用WebService,以及JS把单斜杠转换成双斜杠

2012年05月26日 ⁄ 综合 ⁄ 共 3598字 ⁄ 字号 评论关闭

使用场景如下:

调用WebService文件上传。

首先是全路径问题,ie可以自动获取,但firefox不行,只能得到文件名,没有路径。

于是上网找解决办法:

解决代码如下:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    
<title></title>
     
<script type="text/javascript" src="jquery-ui-1.8.14.custom/js/jquery-1.5.1.min.js"></script>
     
<script type="text/javascript">
         
function getFullPath(obj) {
             
if (obj) {
                 
//ie
                 if (window.navigator.userAgent.indexOf("MSIE">= 1) {
                     obj.select();
                     
return document.selection.createRange().text;
                 }
                 
//firefox
                 else if (window.navigator.userAgent.indexOf("Firefox">= 1) {
                     
if (obj.files) {
                         
return obj.files.item(0).getAsDataURL();
                     }
                     
return obj.value;
                 }
                 
return obj.value;
             }
         } 
     
</script>
     
     
    
<script type="text/javascript">
        $(
function() {
            $(
"#upload").click(function() {
                
var a = $("#fileName").val();
                
var aa = getFullPath(document.getElementById("fileName"));
                
//alert(aa);
                var b = /\\/gi;
                
var c = a.replace(b, "\\\\");
                $.ajax({
                    type: 
"POST",
                    
//contentType: "application/json",
                    url: "WebService.asmx/uploadfile",
                    data: 
"name=" + c,
                    
//dataType: 'json',
                    success: function(result) {
                        
var d = $(result).find("string");
                        alert(d.text());
                    },
                    error: 
function(textStatus, errorThrown) {
                        alert(
"出错了" + this.errorThrown);
                    }

                });
            });
        })
           
        
    </script>
</head>
<body>
<div>
    
<input type="file" id="fileName" value="" /><input type="button" id="upload" value="上传" />
    
</div>
</body>
</html>

webService代码

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;

/// <summary>
///WebService 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo 
= WsiProfiles.BasicProfile1_1)]
//若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。 
// [System.Web.Script.Services.ScriptService]
public class WebService : System.Web.Services.WebService {

    public WebService () {

        //如果使用设计的组件,请取消注释以下行 
        
//InitializeComponent(); 
    }

    [WebMethod]
    public string HelloWorld() {
        
return "Hello World";
    }

    [WebMethod]
    public string uploadfile(string name)
    {
        
string n = name;
        
return n;
    }
    
}

不过,有很多问题:

首先,如果路径名是var  a="c:\1.txt"。经过正则后,结果值是c:1.txt。没有了斜杠。把路径名放在input 的value里面才有效。

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<script type="text/javascript">
function change(){
    
var a = document.getElementById("old").value;
    a 
= "F:\source code\DsPrn\CPort";
    
var mytype = typeof (a);    
    
var b = /\\/gi;
    
var c = a.replace(b,'\\\\');
    document.getElementById(
"new").value = c;
    alert(a);
}
</script>
<body>
原字符串:
<input id="old" value="F:\source code\DsPrn\CPort" type="text" style="width:400px;"/><br/>
新字符串:
<input  id="new" type="text" style="width:400px;"/><br/>
 
<input type="button" value="转换" onclick="change()"/>
</body>
</html>

抱歉!评论已关闭.