JQuery调用WebService,以及JS把单斜杠转换成双斜杠
2011-07-25 15:47
375 查看
使用场景如下:
调用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>
调用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>
相关文章推荐
- JS,Jquery 调用 C#WebService
- JS-JQuery(JSONP)调用WebService跨域若干技术点
- 原生js,jquery ajax请求以及jsonp的调用
- 解决vscode中自动格式化stylus以及js自动将单引号转换成双引号
- 新建WebService供JQuery调用及用js判断是否要调用服务器的submit
- (转)jquery跨域调用webService,以及解决跨域情况下只能返回XMl格式数据的问题
- Json对象与Json字符串互转(4种转换方式) jquery 以及 js 的方式
- Json对象与Json字符串互转(4种转换方式) jquery 以及 js 的方式
- JQuery调用VS2005 c#2.0编写的Webservice时返回值转换成JSON格式的解决办法
- android学习笔记---53_采用网页设计软件界面,以及使用android系统内置的浏览器,利用js调用java方法
- 抛弃WebService 在.NET4中jQuery调用WCF
- 动态调用WebService以及传递复杂对象
- (webservice,ajax,jmail)Tip:动态调用webservice,ajaxpro的使用要点以及使用jmail接口使用注意事项
- jQuery调用WebService
- C++中实现WebService以及调用WebService
- java 实现WebService 以及不同的调用方式
- Jquery 远程调用 webService报错,500错误
- Cookie操作以及如何在js中调用jsp变量
- JQuery对象与js对象的相互转换方法总结
- jquery中通过ajax调用webservice传递数组参数的问题