var Upload = function (file) {
this.file = file;
};
$("#csvfile").on("change", function (e) {
var file = $(this)[0].files[0];
var upload = new Upload(file);
upload.doUpload();
});
Upload.prototype.doUpload = function () {
var myFileList = document.getElementById('csvfile').files;
var myFile = myFileList[0];
var myFileName = myFile.name;
var myFileSize = myFile.size;
var myFileType = myFile.type;
alert("FileName: " + myFileName + "- FileSize: " + myFileSize + " - FileType: " + myFileType);
var formData = new FormData();
formData.append('my_uploaded_file', myFile);
var xhr = new XMLHttpRequest();
xhr.open("POST", 'uploadcsv.php');
xhr.send(formData);
var fileFullpath="'C:\\\\"+ myFileName + "'";
var query = "LOAD DATA LOW_PRIORITY LOCAL INFILE " + fileFullpath + " INTO TABLE tb_Name CHARACTER SET cp1256 FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY ',' LINES TERMINATED BY '\r\n' IGNORE 1 LINES (Field1, Field2, Field3, Field4);";
$.ajax({
type:'POST',
url:'insertData.php',
async:false,
data:{'data':query},
success: function(data){
if(data){
$("#status").append("<br />Data added successfuly");
}else{
$("#status").append("<br />Something went wrong, can't edit the row. ");
}
}
});
$.ajax({
type: "POST",
url: 'deletecsv.php',
data: {'file' : myFileName },
success: function (response) {
alert("deleted");
},
error: function () {
alert("can't delete file csv");
}
});
};
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form method="post" id="fileinfo" name="fileinfo" onsubmit="return submitForm();">
Upload CSV file :
<input type="file" class="form-control" name="csvfile" id="csvfile" />
</form>
<div id="status"></div>