所以我的問題是這樣的:
1.我有一個HTML頁面,它使用JQuery文件對話框,並要求用戶上傳CSV文件。
2.我有一個由名稱的外部JS文件名爲dialogCalled()函數chart_edit.js如何將文件從文件對話框發送到外部JS?
我的代碼的功能是這樣的:
1.在該文件對話框的文件上傳成功,上點擊「OK」按鈕,我想調用另一個對話框(我們稱之爲dialog2),它顯示用戶上傳的CSV數據轉換爲表格格式並顯示在dialog2上。
2.所以在下面給出的代碼中,按下OK按鈕,我希望調用外部js文件中的dialogCalled()函數。但是,我無法將文件對話框中選定的文件發送到外部js。我總是會得到一個錯誤,提示「Uncaught reference:dialogCalled is not defined」
我應該如何將文件對話框中的文件發送到外部js函數或者有其他方法嗎?
代碼:
1.在我的HTML文件的文件對話框:
$.FileDialog({multiple: false}).on('files.bs.filedialog', function(ev) {
files = ev.files;
if(files.length>1){
alert('More than 1 file not allowed');
}
else {
var text = "";
files.forEach(function(f) {
text += f.name + "<br/>";
});
$("#output").html(text);
//DRAWING CHART AFTER CLICK OK
$('#chartArea').empty();
var apiData = new FormData();
apiData.append('file', files[0]);
$("#loading").show();
$("#myModal").css("display","block");
dialogCalled(files);
}
//.DRAWING CHART AFTER CLICK OK
}).on('cancel.bs.filedialog', function(ev) {
$("#output").html("Cancelled!");
});
}
ID爲 「myModal」 是包含dialog2股利。
dialogCalled函數()存儲在外部JS文件:
function dialogCalled(object f){ var files = f; d3.text(files, function(datasetText) { var parsedCSV = d3.csv.parseRows(datasetText); var tbl = d3.select(".modal1-body") .append("table") .attr("class","data-table") .attr("id","data-table"); // headers tbl.append("thead").append("tr") .selectAll("th") .data(parsedCSV[0]) .enter().append("th") .attr("class",function(d,i){return "col"+(i+1);}) .attr("id",function(d,i){return i+1}) .text(function(d) { return d+" "; }) .append("input").attr("type","checkbox").attr("class","form-checkbox").attr("id",function(d,i){return "col" + (i+1)}); // data tbl.append("tbody") .selectAll("tr").data(parsedCSV.slice(1)) .enter().append("tr") .attr("id",function(d,i){return "row"+(i+1);}) .selectAll("td") .data(function(d){return d;}) .enter().append("td") .text(function(d){return d;}) .attr("class",function(d,i){return "col" + (i+1)}) }); }
能有人幫我身邊?
謝謝......這部分工作正常!但是我遇到了一個新的問題,那就是文件沒有被髮送到外部的js。我得到這個錯誤 - http:// localhost/folder_name/views/[object%20File] 404(Not Found) –
看來你的網址不正確?可能是端口號缺失,請檢查網址是否正確。 –