[英]JavaScript: Convert uploaded CSV file contents to Array
我需要一些有關使用JavaScript代碼將上傳的csv文件內容轉換為數組的幫助。
以下是我到目前為止所做的:
我有一個csv文件,其中包含所有clientID的列表,例如
ClientIDs
1
2
3
使用以下html代碼,我上傳了文件
<form id = "FileUpload" method="post" enctype="multipart/form-data">
<div> <p><p>
<b>Select file containing all clientIDs:</b>
<input type="file" id="FileUpload" name="csv"/>
<input type="hidden" name="action" value="upload"/>
<input type="submit" name="FileUpload" value="Upload File"/>
</div>
</form>
現在,我被困的部分。 讀取並轉換文件內容的JS
if(document.getElementById("FileUpload").value != "") {
console.log("The file was uploaded");
//Process the file and convert its contents to array
}
有人可以幫忙嗎?
提前致謝。
假設ID用新行分隔,您可以執行以下操作:
var reader = new FileReader();
reader.onload = function (e) {
var arr = e.target.result.split("\n");
// Continue processing...
};
reader.readAsText(document.getElementById("FileUpload").files[0]);
另請參閱: https : //developer.mozilla.org/en/docs/Web/API/FileReader
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.