I have to download .zip file (created using python shutil library) using FastAPI Server.
Code to create .zip file
shutil.make_archive(base_name = base_name ,
format = 'zip',
root_dir = 'fileName.zip')
Here is my route method.
@app.get("/_archiveFiles_download", response_class = FileResponse)
async def archiveFiles_download(request: Request):
archiveFilePath = 'fileName.zip'
return FileResponse(path = archiveFilePath)
And JavaScript function
// function to download archive file
function downloadZipFiles() {
$.get("./archiveFiles_download").done(function (data) {
console.log(data) // prints garbage in console window
// Create an anchor element for the download link
const link = document.createElement("a");
const blob = new Blob([data1], { type: "application/x-zip-compressed" });
const url = URL.createObjectURL(blob);
link.href = url;
link.download = 'fileName_Download.zip'; // Set the desired file name
link.click(); // Clean up the temporary URL
URL.revokeObjectURL(url);
});
}`
console.log(data) prints garbage data in the inspect console.
also downloaded file is corrupted.
Same thing happens if I send .txt file having some special characters.