function upload_image(e, template) {
$("#loader_class").removeClass("div_hide_class");
if (e.currentTarget.files && e.currentTarget.files[0]) {
var file = e.currentTarget.files[0];
var chunkSize = Infinity;
var options = {
endpoint: "http://13.232.101.233:8000/uploads",
chunkSize: chunkSize,
metadata: {
filename: file.name,
user_id: "Ankit",
},
header: {
"key":"value"
},
beforeSend: function(xhr){xhr.setRequestHeader('X-Test-Header', 'test-value');},
onError: function(error) {
if (error.originalRequest) {
if (confirm('Failed because: ' + error + '\nDo you want to retry?')) {
options.resume = false;
options.uploadUrl = upload.url;
upload = new tus.Upload(file, options);
upload.start();
return;
}
} else {
alert('Failed because: ' + error);
}
},
onProgress: function(bytesUploaded, bytesTotal) {
var percentage = (bytesUploaded / bytesTotal * 100).toFixed(2);
console.log(bytesUploaded, bytesTotal, percentage + '%');
},
onSuccess: function(data) {
var uploaded_url = upload.url;
var final_url = uploaded_url.replace("8000","3000");
alert(final_url);
var type = file.type;
if(type.includes("video")) {
Session.set("post_type","video");
Session.set("media_url",final_url);
Session.set("video_url",final_url);
var vid = document.getElementById("my_video_1");
vid.src = final_url;
}
else if(type.includes("image")){
Session.set("post_type","image");
Session.set("media_url",final_url);
$("#post_image").attr("src",final_url);
}
else if(type.includes("audio")){
Session.set("post_type","audio");
Session.set("media_url",final_url);
$("#audio_file").attr("src",final_url);
}
else{
if(type.includes("powerpoint")){
Session.set("post_type","powerpoint_doc");
$("#document_image").attr("src","/images/ppt.svg");
}else if(type.includes("msword")){
Session.set("post_type","word_doc");
$("#document_image").attr("src","/images/docx.svg");
}else if(type.includes("pdf")){
Session.set("post_type","pdf_doc");
$("#document_image").attr("src","/images/pdf.svg");
}
Session.set("media_url",final_url);
$("#document_div").removeClass("div_hide_class");
}
var post_type = Session.get("post_type");
updateViews(Session.get("post_type"));
$("#loader_class").hide();
}
};
upload = new tus.Upload(file, options);
upload.start();
}
}
Comments
Post a Comment