【发布时间】:2021-07-06 17:04:11
【问题描述】:
我试图在 react 中显示一个 base 64 编码的图像,它没有在网页上显示图像,它只显示一个轮廓。我将如何显示图像?
Spring Boot 后端
public void addNewBlog(String title, String description, String body, String author, MultipartFile image) throws IOException {
String fileName = StringUtils.cleanPath(image.getOriginalFilename());
Blog blog = new Blog(title, "", description, body, author);
blog.setName(fileName);
if(fileName.contains(".."))
{
System.out.println("not a valid file");
}
try {
blog.setContent(Base64.getEncoder().encodeToString(image.getBytes()));
} catch (IOException e) {
e.printStackTrace();
}
blog.setSize(image.getSize());
blog.setUploadTime(new Date());
blogRepository.save(blog);
}
图片
<img src={`data:image/jpeg;base64,${blog.content}`} style={{width: 100, height: 100 }} />
【问题讨论】:
-
src属性在元素检查器中是否包含blog.content值? -
@TheHeadRush 是的。
标签: java reactjs spring spring-boot spring-mvc