我是想做个上传附件带预览图片功能的 ,但传入的参数老是丢失'\'.我把完整代码发下....
希望高手可以帮看看怎么解决
<script language="javascript" type="text/javascript">// 预滥图片function PreviewImg(imgFile) {//imgFile 是传入的完整路径..就是丢失'\'的地方alert(imgFile);
var newPreview = document.getElementById("newPreview");
var imgDiv = document.createElement("div");
document.body.appendChild(imgDiv);
imgDiv.style.width = "118px"; imgDiv.style.height = "127px";
imgDiv.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod = scale)";imgDiv.filters.item("DXImageTransform.Microsoft.AlphaImageLoader").src = imgFile;
newPreview.appendChild(imgDiv);
var showPicUrl = document.getElementById("showPicUrl");
//showPicUrl.innerText = imgFile;
newPreview.style.width = "80px";
newPreview.style.height = "60px"; }//监视是否选择了文件function OnChang() {var temp1 = document.getElementById('FileUpload');
var FullPath = temp1.value;
// alert(FullPath);
if (FullPath == "")
return;//取出短文件名
var fileName = FullPath.substring(FullPath.lastIndexOf("\\") + 1);
// alert(FullPath);
//取得文件扩展
var fileExt = fileName.substring(fileName.lastIndexOf(".") + 1);var table = document.getElementById('filterName');
var pos = table.rows.length;// 添加一行
var otr = table.insertRow(pos);
var cell1 = otr.insertCell(0);cell1.innerHTML = "<input id='Button1' type='button' value='[添加]' /><input id='Button2' type='button' value='[删除]' /><input id='Button3' type='button' value='[" + fileName + "]' onmousemove=\" PreviewImg('" + FullPath + "')\" />"
// 下面是之前一个朋友的方法,是可以显示图片的但不需要鼠标移动到上面去
// document.getElementById("Button3").onclick = PreviewImg(FullPath);
}</script>
//////////////////////////////////////////////页面代码////////////////////////////////////
<table id="filterName" border="0" cellpadding="0" cellspacing="0"></table>
<div id=newPreview></div>
<div id=showPicUrl></div>
<p>
<input id="FileUpload" type="file" size="20" onchange="OnChang() " /></p>

解决方案 »

  1.   


     if(str!="" || str!="null" || str.length<4 || str!=null){
       if(limitfile!="error"){
         var lastname = str.substring(pos,str.length)  //此处文件后缀名也可用数组方式获得str.split(".")
         if (limitfile.indexOf(lastname.toLowerCase())<0)
             {
                 alert("您上传的文件类型为"+lastname.toLowerCase()+",本上传必须为"+limitfile+"类型");
                 //document.myform.pic.focus();
                 return false;
             }
             else 
             {
              alert("文件类型符合规定!");
              return true;
             }
        }else{
          document.getElementById("types").value =0;
        }
    }
      

  2.   

    主要问题是:在JS里 \ 是个转义字符,你要把他替换成 \\才能行
    测试HTML代码如下:<!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>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>无标题文档</title>
    <script language="javascript" type="text/javascript"> // 预滥图片 function PreviewImg(imgFile) { //imgFile 是传入的完整路径..就是丢失'\'的地方 alert(imgFile); 
    var newPreview = document.getElementById("newPreview"); 
    var imgDiv = document.createElement("div"); 
    document.body.appendChild(imgDiv); 
    imgDiv.style.width = "118px"; imgDiv.style.height = "127px"; 
    imgDiv.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod = scale)"; imgDiv.filters.item("DXImageTransform.Microsoft.AlphaImageLoader").src = imgFile; 
    newPreview.appendChild(imgDiv); 
    var showPicUrl = document.getElementById("showPicUrl"); 
    //showPicUrl.innerText = imgFile; 
    newPreview.style.width = "80px"; 
    newPreview.style.height = "60px"; } //监视是否选择了文件 function OnChang() { 
    var temp1 = document.getElementById('FileUpload'); 
    var FullPath = temp1.value; 
    // alert(FullPath); 
    if (FullPath == "") 
    return; 
    alert(FullPath);
    //FullPath=FullPath.replaceAll("\\","\\\\");
    //str=str.replace(/(hand)/g,"hand.gif");
    FullPath=FullPath.replace(/\\/g,"\\\\");
    alert(FullPath);
    //取出短文件名 
    var fileName = FullPath.substring(FullPath.lastIndexOf("\\") + 1); 
    // alert(FullPath); 
    //取得文件扩展 
    var fileExt = fileName.substring(fileName.lastIndexOf(".") + 1); var table = document.getElementById('filterName'); 
    var pos = table.rows.length; // 添加一行 
    var otr = table.insertRow(pos); 
    var cell1 = otr.insertCell(0); cell1.innerHTML = " <input id='Button1' type='button' value='[添加]' /> <input id='Button2' type='button' value='[删除]' /> <input id='Button3' type='button' value='[" + fileName + "]' onmousemove=\" PreviewImg('" + FullPath + "')\" />" 
    // 下面是之前一个朋友的方法,是可以显示图片的但不需要鼠标移动到上面去 
    // document.getElementById("Button3").onclick = PreviewImg(FullPath); 
    } </script> </head><body>
    <table id="filterName" border="0" cellpadding="0" cellspacing="0"> </table> 
    <div id=newPreview> </div> 
    <div id=showPicUrl> </div> 
    <p> 
    <input id="FileUpload" type="file" size="20" onchange="OnChang() " /> </p> 
    </body>
    </html>
      

  3.   

    是的,是丢失'\'导致的...
       谢谢weifei_tlp 按照你说的改了就没问题了....
      谢谢了~~~