碰到的问题场景:
上传了个文件,但文件名称中含有特殊字符,例如&,#.
现在要将这个字符转换成URL认识的样子,并且不影响下载后的文件名称
function getContextPath() {
var pathName = document.location.pathname;
var index = pathName.substr(1).indexOf("/");
var result = pathName.substr(0,index+1);
return result;
}
function formatUrl(url){
url = url.replace(/&/g,"%26");
url = url.replace(/#/g,"%23");
//url = escape(url);
alert(url);
url = getContextPath()+"/FileDownLoadService?filename="+url;
alert(url);
var elemIF = document.createElement("iframe");
elemIF.src = url;
elemIF.style.display = "none";
document.body.appendChild(elemIF);
//alert('xx');
//window.open(url);
}
js替换特殊字符及取小数点处理
function Check(s) {2 var str = s.replace(/%/g, "%25 ").replace(/\+/g, "%2B ").replace(/\s/g, "+ "); // % + \s 3
str = str.replace(/-/g, "%2D ").replace(/\*/g, "%2A ").replace(/\//g, "%2F "); // - * / 4
str = str.replace(/\&/g, "%26 ").replace(/!/g, "%21 ").replace(/\=/g, "%3D "); // & ! = 5
str = str.replace(/\?/g, "%3F ").replace(/:/g, "%3A ").replace(/\|/g, "%7C "); // ? : | 6
str = str.replace(/\,/g, "%2C ").replace(/\./g, "%2E ").replace(/#/g, "%23 "); // , . # 7
return str;8 }
小数点特殊处理
var Digit = {}; 2 /** 3 * 四舍五入法截取一个小数 4 *
@param float digit 要格式化的数字 5 *
@param integer length 要保留的小数位数 6 *
@return float 7 */
<br /> 8
Digit.round = function(digit, length) { 9
length = length ? parseInt(length) : 0;10
if (length <= 0) return Math.round(digit);11
digit = Math.round(digit * Math.pow(10, length)) / Math.pow(10, length);12
return digit;13
};14 /**15 * 舍去法截取一个小数16 * @param float digit 要格式化的数字17 * @param integer length 要保留的小数位数18 * @return float19 */20
Digit.floor = function(digit, length) {21
length = length ? parseInt(length) : 0;22
if (length <= 0) return Math.floor(digit);23
digit = Math.floor(digit * Math.pow(10, length)) / Math.pow(10, length);24
return digit;25 };26 /**27 * 进一法截取一个小数28 * @param float digit 要格式化的数字29 * @param integer length 要保留的小数位数30 * @return float31 */32
Digit.ceil = function(digit, length) {33
length = length ? parseInt(length) : 0;34
if (length <= 0) return Math.ceil(digit);35
digit = Math.ceil(digit * Math.pow(10, length)) / Math.pow(10, length);36
return digit;37 };38 39 // 使用方法40
var num = 1.2345;41
document.write("数字:" + num + "<br />");42
document.write("四色五入法:<br />");43
document.write(num + " 保留3位小数:" + Digit.round(num, 3) + "<br />");44
document.write(num + " 保留2位小数:" + Digit.round(num, 2) + "<br />");45
document.write("舍去法:<br />");46
document.write(num + " 保留3位小数:" + Digit.floor(num, 3) + "<br />");47
document.write(num + " 保留2位小数:" + Digit.floor(num, 2) + "<br />");48
document.write("进一法:<br />");49
document.write(num + " 保留3位小数:" + Digit.ceil(num, 3) + "<br />");50
document.write(num + " 保留2位小数:" + Digit.ceil(num, 2) + "<br />")
连接地址编写方式
<a href="javascript: formatUrl('${att.filename}')"