File tree Expand file tree Collapse file tree 2 files changed +32
-2
lines changed Expand file tree Collapse file tree 2 files changed +32
-2
lines changed Original file line number Diff line number Diff line change @@ -57,9 +57,28 @@ const ImageUpload = ({ onImageUpload }) => {
57
57
}
58
58
} ;
59
59
60
+ const deleteImage = async ( filename ) => {
61
+ try {
62
+ const response = await axios . delete ( `${ import . meta. env . VITE_SERVER_URL } /uploads/${ filename } ` ) ;
63
+ showSnackbar ( response . data . message , 'success' ) ;
64
+
65
+ // Update the state to remove the deleted image
66
+ const updatedImages = images . filter ( ( image ) => image . filename !== filename ) ;
67
+ setImages ( updatedImages ) ;
68
+ onImageUpload ( updatedImages ) ;
69
+ } catch ( error ) {
70
+ if ( error ?. response ?. data ) {
71
+ showSnackbar ( error . response . data . message , 'error' ) ;
72
+ } else {
73
+ showSnackbar ( "Couldn't connect to the server" , 'error' ) ;
74
+ }
75
+ console . error ( 'Error deleting image:' , error ) ;
76
+ }
77
+ } ;
78
+
60
79
const handleRemoveImage = ( index ) => {
61
- setImages ( ( prevImages ) => prevImages . filter ( ( _ , i ) => i !== index ) ) ;
62
- onImageUpload ( images . filter ( ( _ , i ) => i !== index ) ) ;
80
+ const imageToRemove = images [ index ] ;
81
+ deleteImage ( imageToRemove . filename ) ;
63
82
} ;
64
83
65
84
const { getRootProps, getInputProps, isDragActive } = useDropzone ( {
Original file line number Diff line number Diff line change @@ -101,7 +101,18 @@ def upload_file():
101
101
def uploaded_file (filename ):
102
102
return send_from_directory (app .config ['UPLOAD_FOLDER' ], filename )
103
103
104
+ @app .route ('/uploads/<filename>' , methods = ['DELETE' ])
105
+ def delete_file (filename ):
106
+ try :
107
+ file_path = os .path .join (app .config ['UPLOAD_FOLDER' ], filename )
108
+ if os .path .exists (file_path ):
109
+ os .remove (file_path )
110
+ return jsonify ({"status" : "success" , "message" : "File deleted successfully" }), 200
111
+ else :
112
+ return jsonify ({"status" : "error" , "message" : "File not found" }), 404
104
113
114
+ except Exception as e :
115
+ return jsonify ({"status" : "error" , "message" : str (e )}), 500
105
116
106
117
@app .route ('/activeImage' , methods = ['POST' ])
107
118
@cross_origin (origin = client_url , headers = ['Content-Type' ])
You can’t perform that action at this time.
0 commit comments