zoukankan      html  css  js  c++  java
  • JQuery调用WebService,以及JS把单斜杠转换成双斜杠

    使用场景如下:

    调用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="400px;"/><br/>
    新字符串:
    <input  id="new" type="text" style="400px;"/><br/>
     
    <input type="button" value="转换" onclick="change()"/>
    </body>
    </html>

    合乎自然而生生不息。。。
  • 相关阅读:
    从零入门 Serverless | Serverless Kubernetes 应用部署及扩缩容
    从单体迈向 Serverless 的避坑指南
    从零入门 Serverless | 教你使用 IDE/Maven 快速部署 Serverless 应用
    开发函数计算的正确姿势——OCR 服务
    从零入门 Serverless | 函数计算的开发与配置
    全部满分!阿里云函数计算通过可信云21项测试
    登录接口+三方登录 微博
    注册接口文档
    异步发送短信验证与 注册接口完善
    图片验证码接口
  • 原文地址:https://www.cnblogs.com/samwu/p/2116270.html
Copyright © 2011-2022 走看看