/    Sign up×
Community /Pin to ProfileBookmark

High waiting time (TTFB) time when uploading file

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?
[upl-image-preview url=https://www.webdeveloper.com/assets/files/2019-06-26/1561560590-935815-im1.png]

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 🙂

to post a comment

0Be the first to comment 😎

×

Success!

Help @0xprateek spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.27,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...