Any workarounds for the Safari HTML5 multiple file upload bug?

后端 未结 3 1389
执笔经年
执笔经年 2021-01-18 11:24

After weeks of tweaking I finally gave up. I just couldn\'t fix my multiple file upload on safari, which really bothered me because my code worked perfectly as it should on

3条回答
  •  失恋的感觉
    2021-01-18 12:21

    It seems that there is another issue that can be messing around. iOS Safari multiple file uploads always use the name "image.jpg" for all uploaded files. It may seem only one file uploaded in the server side but this is not what happened: it has been uploaded all files with the same name!

    So, the workaround comes into the server side: just change the destination filename with a new generated name.

    I am working with Flask and Python, so I use the standard formula.

    @app.route("/upload",methods=['POST'])
    def upload():
        files = request.files.getlist('files[]')
        for file in files:
            if file and allowed_file(file.filename):
                filename = generate_filename(file.filename)
                file.save( os.path.join(app.config['UPLOAD_FOLDER'], new_album_id, filename))
        return render_template('upload_ok.html')
    

    Where generate_filaname has to be a function that creates a new filename with the same extension as the original one. For example:

    def generate_filename(sample_filename):
        # pick up extension from sample filename
        ext = sample_filename.split(".")[-1]
        name = ''.join( random.SystemRandom().choice(string.letters+string.digits) for i in range(16) )
        return name + "." + ext
    

    The same can be done in PHP, of course.

提交回复
热议问题