helloflask / flask-dropzone

Upload files in Flask application with Dropzone.js.
https://flask-dropzone.readthedocs.io
MIT License
250 stars 69 forks source link

How to remove local file when remove file button was pressed? #20

Closed Sacchid closed 5 years ago

Sacchid commented 5 years ago

From flask-dropzone/examples/custom-option, whenever remove button is pressed, image (file) is removed from web-view but not from uploads folder. How to check which image is removed by clicking on image button & how to remove it from uploads? And can I temp. save files for session & then based some logic decide to delete them or save permanently.

greyli commented 5 years ago

Sorry for taking so long. This feature is not implemented currently, but your can make it by yourself. First, register the event listeners with JavaScript:

Dropzone.options.myAwesomeDropzone = {
    success: function(file, response) {
        file.serverFileName = response.file_name;  // pass filename
    },
    removedfile: function (file, data) {  // triggered when remove file button was clicked
        $.ajax({  // send AJAX request to Flask to remove file
            type:'POST',
            url:'/deletefile',
            data : {"filename" : file.serverFileName},  // pass filename
            success : function (data) {
                alert('File removed from server.')
            }
        });
    }
};

Then, create the view function to delete file with Flask:

@app.route('/deletefile')
def delete_file():
    filename = request.form['filename']
    file_path = os.path.join(your_upload_path, filename)
    os.remove(file_path)

Not test yet, let me know if it works ;)

greyli commented 5 years ago

Closing for inactive, please feel free to comment or reopen.

A-Shot commented 3 years ago

here is my code can any one let me know what I'm doing wrong?

index2.txt

CFGrote commented 3 years ago

i'm interested in this feature, too. any updates? where exactly am i supposed to paste the js snipped (copied from above)?

Dropzone.options.myAwesomeDropzone = {
    success: function(file, response) {
        file.serverFileName = response.file_name;  // pass filename
    },
    removedfile: function (file, data) {  // triggered when remove file button was clicked
        $.ajax({  // send AJAX request to Flask to remove file
            type:'POST',
            url:'/deletefile',
            data : {"filename" : file.serverFileName},  // pass filename
            success : function (data) {
                alert('File removed from server.')
            }
        });
    }
};