In a flask website, I am trying to upload a file using XHR. File uploading works perfectly but the problem is that on browser side it shows file uploaded 100% but on the server side, the file is not yet uploaded(It is taking extra time to appear). When I checked in Developers tools I found that the file is taking large Waiting time. How I can reduce this waiting time?
XHR Request for file progress update
` var xhr = $.ajax({
xhr: function() {
var xhrobj = $.ajaxSettings.xhr();
if (xhrobj.upload) {
xhrobj.upload.addEventListener(“progress”, function(event) {
var percent = 0;
var position = event.loaded || event.position;
var total = event.total;
if (event.lengthComputable) {
percent = Math.ceil(position / total * 100);
}
// Set the progress bar.
$progressBar.css({“width”: percent + “%”});
//$fileProgress.text(“File Progress : “+percent + “%”);
document.getElementById(‘fileProgress’).innerHTML = “❖ File Progress : “+percent + “% ❖”;
if (percent == 100){
document.getElementById(“done-btn”).disabled = false;
}
}, false)
}
return xhrobj;
},
url: UPLOAD_URL,
method: “POST”,
contentType: false,
processData: false,
cache: false,
data:fd,
success: function(data) {
$progressBar.css({“width”: “100%”});
console.log(“Yuck Yes…!”+data);
},
});`
Flask Function For file Upload
`
@app.route(“/upload”, methods=[‘POST’])
def upload():
target = os.path.join(APP_ROOT, ‘images/’)
print(target)
if not os.path.isdir(target):
os.mkdir(target)
for file in request.files.getlist(“file”):
print(file)
filename = file.filename
destination = “/”.join([target, filename])
print(destination)
file.save(destination)
return ”,204`
Any help will be appriciated