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 () {
// Retrieve the FileList object from the referenced element ID
var myFileList = document.getElementById('csvfile').files;
// Grab the first File Object from the FileList
var myFile = myFileList[0];
// Set some variables containing the three attributes of the file
var myFileName = myFile.name;
var myFileSize = myFile.size;
var myFileType = myFile.type;
// Alert the information we just gathered
alert("FileName: " + myFileName + "- FileSize: " + myFileSize + " - FileType: " + myFileType);
// Let's upload the complete file object
// Open Our formData Object
var formData = new FormData();
// Append our file to the formData object
// Notice the first argument "file" and keep it in mind
formData.append('my_uploaded_file', myFile);
// Create our XMLHttpRequest Object
var xhr = new XMLHttpRequest();
// Open our connection using the POST method
xhr.open("POST", 'uploadcsv.php');
// Send the file
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);";
//INSERT file into DB
$.ajax({
type:'POST',
url:'insertData.php',
async:false, //to make sure all data inserted
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. ");
}
}
});
//Then delete uploaded file after INSERT
$.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>