| 45 | function sendSPARQL2(){ |
| 46 | var ep = $("*[name=selectendpoint]").val(); |
| 47 | |
| 48 | if(ep == "input"){ |
| 49 | ep = $("*[name=inputendpoint]").val(); |
| 50 | } |
| 51 | |
| 52 | var query = $("*[name=sparql]").val().replace(/[\n\r]/g,""); |
| 53 | |
| 54 | //query = encodeURIComponent(query); |
| 55 | |
| 56 | qr = sendQuery(ep,query); |
| 57 | |
| 58 | qr.fail( |
| 59 | function (xhr, textStatus, thrownError) { |
| 60 | alert("Error: A '" + textStatus+ "' occurred."); |
| 61 | } |
| 62 | ); |
| 63 | qr.done( |
| 64 | function (d) { |
| 65 | download_result(d.results.bindings); |
| 66 | } |
| 67 | ); |
| 68 | } |
| 69 | |
| 70 | function download_result(data){ |
| 71 | |
| 72 | if (data instanceof Array) { |
| 73 | var result_txt =""; |
| 74 | |
| 75 | var i=0; |
| 76 | for ( var key in data[0]) { |
| 77 | if(i>0){result_txt +=",";} |
| 78 | result_txt += key; |
| 79 | i++; |
| 80 | } |
| 81 | |
| 82 | result_txt += "\n"; |
| 83 | |
| 84 | for (var d = 0; d < data.length; d++) { |
| 85 | var i = 0; |
| 86 | for ( var key in data[d]) { |
| 87 | if(i>0){result_txt +=",";} |
| 88 | result_txt += data[d][key].value; |
| 89 | i++; |
| 90 | } |
| 91 | result_txt += '\n'; |
| 92 | } |
| 93 | |
| 94 | var blob = new Blob( [result_txt], {type: 'text/plain'} ) |
| 95 | |
| 96 | var link = document.createElement('a') |
| 97 | link.href = URL.createObjectURL(blob) |
| 98 | link.download = 'result' + '.csv' |
| 99 | |
| 100 | document.body.appendChild(link) // for Firefox |
| 101 | link.click() |
| 102 | document.body.removeChild(link) // for Firefox |
| 103 | } |
| 104 | }; |
| 105 | |
| 106 | |