Answers for "flask request.files save zip"

0

flask request.files save zip

@app.route("/upload", methods=["GET", "POST"])
def upload():
    if request.method == "GET":
        return render_template("upload.html")
    obj = request.files.get("file")
    print(obj)  # <FileStorage: "test.zip" ("application/x-zip-compressed")>
    print(obj.filename)  # test.zip
    print(obj.stream)  # <tempfile.SpooledTemporaryFile object at 0x0000000004135160>
         # Check if the suffix name of the uploaded file is zip
    ret_list = obj.filename.rsplit(".", maxsplit=1)
    if len(ret_list) != 2:
                 return "Please upload zip file"
    if ret_list[1] != "zip":
                 return "Please upload zip file"
 
         # Method 1: Save the file directly
    obj.save(os.path.join(BASE_DIR, "files", obj.filename))
 
         # Method 2: Save the decompressed file (the original compressed file is not saved)
    target_path = os.path.join(BASE_DIR, "files", str(uuid.uuid4()))
    shutil._unpack_zipfile(obj.stream, target_path)
 
         # Method three: Save the compressed file locally, then decompress it, and then delete the compressed file
         file_path = os.path.join(BASE_DIR, "files", obj.filename) # The path where the uploaded file is saved
    obj.save(file_path)
         target_path = os.path.join(BASE_DIR, "files", str(uuid.uuid4())) # The path where the unzipped files are saved
    ret = unzip_file(file_path, target_path)
         os.remove(file_path) # delete file
    if ret:
        return ret
    
         return "Upload successful"
Posted by: Guest on July-07-2021

Python Answers by Framework

Browse Popular Code Answers by Language