首页 > 解决方案 > 使用带有 ReactJS 的 axios 从响应 json 动态更新 img 的 src

问题描述

我使用图像对 ML 后端进行 axios post 调用,然后它处理并生成预测图像并将其保存到磁盘并返回新图像的名称。这些新图像的保存位置已经在后端预先设置。所以现在使用图像的名称,我想在反应中更新 img 的 src 属性。

const fd = new FormData();
fd.append('qqfile', files[0], files[0].name);
axios.post('http://localhost:5526/upload', fd)
  .then(res => {
    console.log(res.data.new_image);
    var path = "/Documents//diabetic_detection/static/processed_file/"+res.data.new_image;
    document.getElementById("result").src = ???
  });

files[0] 是我发送到 api 的图像!res.data.new_image 是处理后图像的名称。我想用这个新图像动态更新 id 为“result”的 img 的 src。请帮忙

标签: javascriptjsonreactjsapiaxios

解决方案


推荐阅读