前端根据url下载文件时,浏览器报错如下图
下载代码如下
let a = document.createElement('a')
// 定义下载名称
a.download = item.useTitleName
// 隐藏标签
a.style.display = 'none'
// 设置文件路径
a.href = res.data
// 将创建的标签插入dom
document.body.appendChild(a)
// 点击标签,执行下载
a.click()
// 将标签从dom移除
document.body.removeChild(a)
在网上找了找有说这个报错是由于浏览器没有访问静态曲线的权限,但是我在springboot中进行配置了,具体代码如下
package com.xjc.common.config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
/**
* @ClassName com.xjc.common.config
* @Author bill
* @Describe
* @Date 2023/5/2 9:03
*/
@Configuration
public class WebConfig implements WebMvcConfigurer {
/**
* 图片保存路径,自动从yml文件中获取数据
*/
@Value("${file-save-path}")
private String fileSavePath;
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
/**
* 配置资源映射
* 意思是:如果访问的资源路径是以“/images/”开头的,
* 就给我映射到本机的“E:/images/”这个文件夹内,去找你要的资源
* 注意:E:/images/ 后面的 “/”一定要带上
*/
registry.addResourceHandler("/image/**")
.addResourceLocations("file:"+fileSavePath);
// registry.addResourceHandler("/usr/local/image/**")
// .addResourceLocations("file:"+fileSavePath);
}
}
这个/image/**是我本地的环境,按道理来讲,我已经配置了,但是还是不行,这个如何处理?