2016-11-19 187 views
-1

我有些鏈接到一個表中給出一個網頁,我想通過javascript來下載所有的人裏面的PDF文件,我該怎麼辦的具體DIV /表元素?下載鏈接從所有的PDF在網頁中

編輯:

講清楚,我有一個網頁在這裏,我只是看它在瀏覽器中,我想要做一些JavaScript,它會自動dwonloads所有PDF特定的表內,這就是全部。

+0

你可以給網站的網址。所以它會很容易寫腳本 – jafarbtech

回答

0

有與JavaScript

一個簡單的解決方案

HTML:

<button id="download" name="download" type="button" onclick="download()">Some Text</button> 

的JavaScript:

function download() { 
    window.open("file_1.txt"); 
    window.open("file_2.txt"); 
} 
2

假設您的PDF鏈接被封裝在具有href屬性<a>標籤。你可以做這樣的事情。

var links = document.getElementsByTagName('a'); 
// Do this selection based on your table 

for(var count=0; count<links.length; count++) { 
    var url = links[count].getAttribute('href'); 
    if(url && url.endsWith('.pdf')) { 
     links[count].dispatchEvent(new MouseEvent('click')); 
    } 
} 

對於瀏覽器本身不打開PDF您將在瀏覽器中禁用它。對於chrome,請轉至chrome://plugins並禁用PDF查看器。另外,如果要下載的pdf數量很大,請在連續下載之間添加一些timeInterval。我希望這有助於

2

那麼最後我有我的方式做正確的任務,看到我,如果你想在它看得更遠fiddle。這個腳本會自動完成我以前的問題所要求的一切,並且它會強制下載PDF而不禁用查看器。

代碼:

(function(ElementTypeToDownload, DownloadFromWhere) { 

    // from http://stackoverflow.com/questions/3077242/force-download-a-pdf-link-using-javascript-ajax-jquery and http://stackoverflow.com/questions/16611497/how-can-i-get-the-name-of-an-html-page-in-javascript 
    // Anonymous "self-invoking" function 
    if (!(typeof jQuery === 'function')) { 
    (function() { 
     var startingTime = new Date().getTime(); 
     // Load the script 
     var script = document.createElement("SCRIPT"); 
     script.src = 'https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js'; 
     script.type = 'text/javascript'; 
     document.getElementsByTagName("head")[0].appendChild(script); 

     // Poll for jQuery to come into existance 
     var checkReady = function(callback) { 
     if (window.jQuery) { 
      callback(jQuery); 
     } else { 
      window.setTimeout(function() { 
      checkReady(callback); 
      }, 20); 
     } 
     }; 

     // Start polling... 
     checkReady(function($) { 
     $(function() { 
      var endingTime = new Date().getTime(); 
      var tookTime = endingTime - startingTime; 
     }); 
     }); 
    })(); 
    } 
    // http://facebook.com/anders.tornblad 
    // [email protected] 

    // from http://stackoverflow.com/questions/3077242/force-download-a-pdf-link-using-javascript-ajax-jquery 
    function SaveToDisk(fileURL, fileName) { 
    // for non-IE 
    if (!window.ActiveXObject) { 
     var save = document.createElement('a'); 
     save.href = fileURL; 
     save.target = '_blank'; 
     save.download = fileName || fileURL.split("/").pop() || 'unknown'; 

     var evt = new MouseEvent('click', { 
     'view': window, 
     'bubbles': true, 
     'cancelable': false 
     }); 
     save.dispatchEvent(evt); 

     (window.URL || window.webkitURL).revokeObjectURL(save.href); 
    } 

    // for IE < 11 
    else if (!!window.ActiveXObject && document.execCommand) { 
     var _window = window.open(fileURL, '_blank'); 
     _window.document.close(); 
     _window.document.execCommand('SaveAs', true, fileName || fileURL) 
     _window.close(); 
    } 
    } 

    // from http://stackoverflow.com/questions/4623982/test-if-jquery-is-loaded-not-using-the-document-ready-event 
    function preInit() { 
    // wait until jquery is loeaded 
    if (!(typeof jQuery === 'function')) { 
     window.setTimeout(function() { 
     //console.log(count++); 
     preInit(); 
     }, 10); // Try again every 10 ms.. 
     return; 
    } 

    $(ElementTypeToDownload || prompt("Enter the type of element you are going to download, e.g. 'a' for link, 'img' or 'image' for images (do not add quotes, deafult is 'a') ") || 'a', DownloadFromWhere || prompt("jQuery Selector for the place that you want to download content from, e.g. '#DivName', '.ClassName', 'TagName', body by deafult") || 'body').each(function() { 
     var tag = (ElementTypeToDownload == 'img' || ElementTypeToDownload == 'image') ? 'src' : 'href'; 
     SaveToDisk($(this).attr(tag)); 
    }); 
    } 

    preInit(); 
})(); 

無論如何謝謝你爲你們幫助

+0

如果你已經完成了,那麼爲什麼要問? – kumardeepakr3

+1

我問了,因爲我發現不知道如何實現結果,但是當我搜索併合並了多個SO問題時,我發現它,所以我自己做了一個。無論如何非常感謝您的幫助。 –

1

下面創建您的數據的按鈕點擊它。點擊所有pdf文件將開始下載。

請確保多個下載您的瀏覽器限制。 鉻一旦你下載點擊所有的按鈕,你必須做出 確保您單擊允許將在 中給出的彈出窗口攔截器地址欄右側的

function clickall(){ 
 
    var pdfs = document.getElementsByClassName('mydata')[0].getElementsByTagName('a'); 
 
    for(var i=0; i<pdfs.length; i++) { 
 
    pdfs[i].setAttribute("download", "download"); 
 
    pdfs[i].click(); 
 
    } 
 
} 
 

 
function excuteDomReadyCallBacks(){ 
 
    var mydata=document.getElementsByClassName('mydata')[0]; 
 
    var element = document.createElement('button'); 
 
       element.onclick = 'clickall()'; 
 
       element.value = 'download all'; 
 
       element.type = 'button'; 
 
    mydata[0].parent.insertAfter(element, mydata); 
 
    } 
 
    document.addEventListener('DOMContentLoaded', function() { 
 
excuteDomReadyCallBacks(); 
 
    //.. do stuff .. 
 
}, false);
<table class="mydata"> 
 
<tbody> 
 
<tr> 
 
<td><b>Name</b></td> 
 
<td><b>City</b></td> 
 
<td><b>State</b></td> 
 
</tr> 
 
<tr> 
 
<td>Greater Noida Cricket Stadium</td> 
 
<td>Greater Noida</td> 
 
<td> 
 
    <a href="http://static.sportskeeda.com/wp-content/uploads/2016/08/hero-isl-2016-fixtures-1472219010.pdf">test2</a> 
 
    </td> 
 
</tr> 
 
<tr> 
 
<td> Buddh International Circuit</td> 
 
<td>Greater Noida</td> 
 
<td> 
 
    <a href="/test2.pdf">test2</a> 
 
</td> 
 
</tr> 
 
<tr> 
 
<td> Buddh International Circuit</td> 
 
<td>Greater Noida</td> 
 
<td> 
 
    <a href="http://www.omri.org/sites/default/files/opl_pdf/CompleteCompany-NOP.pdf">test2</a> 
 
</td> 
 
</tr> 
 
</tbody> 
 
</table>