实现在前端删除存储在服务器中的图片的功能,代码仅供参考
后端代码:
import os
# 图片删除接口实现
@goods.route('/delete_img/',methods=['DELETE'])
def delete_img():
path = request.json.get('path')
if path:
imgname = os.path.basename(path)
img_path = os.path.join(current_app.config.get('SERVER_IMG_UPLOADS'),imgname)
for filename in os.listdir(current_app.config.get('SERVER_IMG_UPLOADS')):
if filename == imgname:
os.remove(img_path)
return to_status_msg(200,path,'图片删除成功')
return to_status_msg(20007,path,'服务器中没有此图片')
return to_status_msg(10000,msg='缺少图片路径参数')
前端代码:
// 改进的方法
async handleRemove(file) {
const { data: res } = await this.$axios.delete('/delete_img/', {
data: { path: file.response.data.path }
})
if (res.status !== 200) this.$msg.error(res.msg)
this.$msg.success(res.msg)
const i = this.addForm.pics.findIndex(x => x === file.response.data.path)
this.addForm.pics.splice(i, 1)
}