php - upload file using ajax temporary save in table -
in project insert data files , other info using ajax, saving of file want insert many files did have table store file content as
var file = $('#file')[0].files[0]; //var file = document.getelementbyid('file').files[0]; var name = file.name; var size = file.size; var type = file.type; var newname = $('#filename').val(); var newrow = "<tr>" + "<td id=''>" + file.name + "</td>" + "<td id=''>" + file.size + "</td>" + "<td id=''>" + file.type + "</td>" + "<td id=''>" + newname + "</td>" + "<td>" + "<a href='#' id='" + id + "' class='deletefile'>delete</a>" + "</td>" + "</tr>"; $("#filetable > tbody > tr:last").after(newrow);
this how save file content in table can insert many files in database allows me rename file using newname
.
after push
content of table in variable(i didnt include in push
how got data table because long). put variable in formdata
$('#addtotable').click(function() { var $rows = $('#filetable').find("tr:not(:eq(0))"); var valuetopush = []; $rows.each(function() { var $tds = $(this).find('td'); var nameoffile = $tds.eq(0).text(); var size = $tds.eq(1).text(); var type = $tds.eq(2).text(); var filename = $tds.eq(3).text(); valuetopush.push({ nameoffile: nameoffile, size: size, filename: filename, type: type }); }); var formdata = new formdata(); formdata.append('file', json.stringify(valuetopush));
note. included append data file
the ajax this
$.ajax({ url: '../include/add.php', type: 'post', datatype: "json", data: formdata, processdata: false, // tell jquery not process data contenttype: false, // tell jquery not set contenttype success: function(data) { console.log(data); //alert(data.message); //window.location.reload(true); }, error: function(data) { console.log(data); //alert("error!"); // optional //window.location.reload(true); } });
and in php have condition
if (!empty($_files)) {
my question why $_files
empty when try echo variable ($_post['file'])
return correct content of file
current file name
, file size
, file type
Comments
Post a Comment