I'm creating a YouTube Video Downloader for which I use flask for the backend. I send an ajax request to the flask in return a flask return file which by default set to download. The file starts downloading but the problem is that there is no responseText or statusCode in the success function in ajax
Ajax Function which I used to work in the same way as jquery ajax does
My Ajax code is
let formData = new FormData(downloadform);
ajax({
url: '/download',
method: "POST",
data: formData,
onprogress: () => {
download_loadingBtn.style.display = "block";
downloadBtn.style.display = "none";
},
onsuccess: (responseText, status, statusText) => {
download_loadingBtn.style.display = "none";
downloadBtn.style.display = "block";
console.log(responseText, status, statusText);
},
onfailure: (status, statusText) =>{
console.log(status, statusText);
}
})
My Flask code is
@app.route("/download", methods = ["GET", "POST"])
def download_video():
if request.method == "POST":
buffer = BytesIO()
url = YouTube(session['link'])
itag = request.form.get("itag")
video = url.streams.get_by_itag(itag)
video.stream_to_buffer(buffer)
buffer.seek(0)
return send_file(buffer, as_attachment=True, download_name=url.title, mimetype="video/mp4")
I also tried to return an object with some response text and send_file() such as
return jsonify({
"text": "successfully downloaded",
"download_file": send_file(buffer, as_attachment=True, download_name=url.title, mimetype="video/mp4")
})
but that also gives an error i.e. TypeError: Object of type Response is not JSON serializable