UPDATE(6/22/2018)
I have programmed a WYSIWYG editor. The area where the text is entered is simply a content editable div. I have encountered problems with the image/upload/file upload button. I want the "Choose File" button to display the video/image in the content editable when uploaded. However, I do not want the uploading to store the image/video/file in the database. Basically, I want that when I insert the image/video, the image/video will display in the content editable. (Update: When I upload an image/video, for the image, a blue square with a white question mark shows up and for the video, nothing shows up except all the other elements are shifted as if an element was actually inserted.)
HTML:
<form id = "upload_form" enctype = "multipart/form-data" method = "post"></form>
<input type = "file" name = "file1" id = "file1"><br>
<input type = "button" value = "Upload File" onclick = "uploadFile()">
<progress id = "progressBar" value = "0" max = "100" style = "width: 300px;"></progress>
<h3 id = "status"></h3>
<p id = "loaded_n_total"></p>
Javascript:
function _(el){
return document.getElementById(el);
}
function uploadFile(){
var file = _("file1").files[0];
//Added
var fileType = _("file1").files[0]['type'];
if(fileType.split('/')[0] == 'image'){
alert("Image");
$('#editor').prepend('<img src= file />')
}
else if(fileType.split('/')[0] == 'video'){
alert("Video");
$('#editor').prepend('<video><source src=file type="video/mp4"></video>');
}
//End of Added
//alert(file.name + " | " + file.size + " | " + file.type);
var formdata = new FormData();
formdata.append("file1", file);
var ajax = new XMLHttpRequest();
ajax.upload.addEventListener("progress", progressHandler, false);
ajax.addEventListener("load", completeHandler, false);
ajax.addEventListener("error", errorHandler, false);
ajax.addEventListener("abort", abortHandler, false);
ajax.open("POST", "file_upload_parser.php");
ajax.send();
}
function progressHandler(event){
_("loaded_n_total").innerHTML = "Uploaded " + event.loaded + " bytes of " + event.total;
var percent = (event.loaded / event.total) * 100;
_("progressBar").value = Math.round(percent);
_("status").innerHTML = Math.round(percent); + "% uploaded ... please wait";
}
function completeHandler(event) {
_("status").innerHTML = event.target.responseText;
_("progressBar").value = 0;
}
function errorHandler(event) {
_("status").innerHTML = "Uploaded Failed";
}
function abortHandler(event) {
_("status").innerHTML = "Uploaded Aborted";
}