2012-09-10 21 views
0

我有一個HTML頁面,列出文件夾和普通文件的操作爲錨標籤中的所有文件名稱旁邊極大,誰用戶點擊一個文件操作說刪除,系統會提示用戶,如果他他肯定會刪除選定的文件。它幾乎完美地工作,但是,如果遇到空格和特殊字符的文件名:(My Track [Ezee] .mp3),它會失敗。是否有解決方法。任何幫助或指針在正確的方向將大大appreciated.Thanks檢索文件名的JavaScript/jQuery的

HTML

<tr class="filetable_entry_alt"> 
<td> 
    <input type="checkbox" name="sample10 with spaces (ezee's).jpg" value="file"> 
</td> 
<td> 
    <a href="/mnt/sdcard/DCIM/sample10 with spaces (ezee's).jpg" title="sample10 with spaces (ezee's).jpg" class="thumb" rel="cb"> 
     <img src="/thumbnail.wft?image=/mnt/sdcard/DCIM/sample10 with spaces (ezee's).jpg&amp;kind=micro&amp;lm=1308828676000" alt=""> 
     <b> 
      <img src="/thumbnail.wft?image=/mnt/sdcard/DCIM/sample10 with spaces (ezee's).jpg&amp;kind=micro&amp;lm=1308828676000" alt="" style="width:150px; height:150px;"> 
     </b> 
    </a> 
</td> 
<td> 
    <a href="/mnt/sdcard/DCIM/sample10 with spaces (ezee's).jpg" title="sample10 with spaces (ezee's).jpg"> 
     sample10 with spaces (ezee's).jpg 
    </a> 
</td> 
<td> 
    06/23/11 12:31 PM 
</td> 
<td> 
    1.76 MB 
</td> 
<td> 
    <a href="#" onclick="return confirmDel('/mnt/sdcard/DCIM/sample10 with spaces (ezee's).jpg', '/mnt/sdcard/DCIM/?');"> 
     delete 
    </a> | 
    <a href="#" onclick="return confirmRen('/mnt/sdcard/DCIM/','sample10 with spaces (ezee's).jpg','/mnt/sdcard/DCIM/?');"> 
     rename 
    </a>| 
    <a href="#" onclick="return confirmCopy('/mnt/sdcard/DCIM/','sample10 with spaces (ezee's).jpg', '/mnt/sdcard/DCIM/?');"> 
     copy 
    </a> 
</td> 

JS代碼

var selectedFile = 'noFile'; 
var selectedFile2 = 'noFile'; 

function confirmDel(t, p) { 

    var confirmDeltxt = 'Are you sure you want to delete ' + t + '?'; 
    selectedFile = decodeURIComponent(t); 
    selectedFile2 = decodeURIComponent(p); 
    jQuery.prompt(confirmDeltxt, { 
     callback: confirmDelcallback, 
     buttons: { 
      Delete: 'ok', 
      Cancel: 'cancel' 
     } 
    }); 
    return false; 

} 

function confirmDelcallback(v, m, f) { 
    if (v != undefined && v == 'ok') { 

     var form = document.forms.filelist; 
     form.action.value = 'delete'; 
     form.data_file.value = selectedFile2 + "/" + selectedFile; 
     form.submit(); 


    } 
} 


function confirmRen(p, f, cp) { 

    selectedFile = decodeURIComponent(p); 
    selectedFile2 = decodeURIComponent(f); 

    var confirmRentxt = 'Enter new file name:<br /><br /><input type="text"  id="newPath" name="newPath" value="' + f + '" />'; 


    jQuery.prompt(confirmRentxt, { 
     submit: confirmRensubmit, 
     callback: confirmRencallback, 
     buttons: { 
      Rename: 'ok', 
      Cancel: 'cancel' 
     } 
    }); 
    return false; 
} 

function confirmRensubmit(v, m, f) { 
    an = m.children('#newPath'); 

    if (v == 'ok') { 
     if (f.newPath == "") { 
      an.css("border", "solid #ff0000 1px"); 
      return false; 
     } 
    } 
    return true; 

} 

回答

2

必須逃離「中的行這樣的,當你輸出HTML。產生的HTML應該是這樣的:

onclick="return confirmCopy('/mnt/sdcard/DCIM/', 
          'sample10 with spaces (ezee\'s).jpg', 
          '/mnt/sdcard/DCIM/?');" 

(新行只是爲了更好的外觀)

您將如何做到這一點取決於你的服務器端語言,但「與\」取代應該是足夠的。

+0

感謝您指出了out.Will整理出來。 – kalz

0

首先,我不知道爲什麼你的/ mnt/SD卡的東西在網頁中玩,但如果你的編輯器有一個語法高亮顯示功能,您會立即看到你的代碼是一個爛攤子。 這裏有一個例子:

<a href="#" onclick="return confirmDel('/mnt/sdcard/DCIM/sample10 with spaces (ezee's).jpg', '/mnt/sdcard/DCIM/?');"> 
    delete 
</a> 

我什至不知道一些人是一個有效的文件路徑。除此之外,你需要做的是逃避角色。例如,

onclick="return confirmDel('/etc/ezze\'s.jpg');" 

我不確定你的jquery代碼,但它看起來也很混亂,它更好,如果你檢查它。

+0

感謝您指出。「/ mnt/sdcard /」,因爲我基於android web服務器上請求的uri,即時生成html頁面。點擊「return confirmDel('filename','filename.getparent()/?')是以編程方式獲取的,所以我相信我必須逃避編程提取的字符串。可能我必須在java中完成。然後在meantime.StringEscapeUtils看起來像一個不錯的選擇。 – kalz

+0

我不知道它有什麼用java做的,但如果這些HTML是由服務器端代碼生成的,而你在服務器端使用Java,那麼,你應該用Java來做。 – hndr