ice 前端代码(我用的是一个拖拽的模板
<Upload.Dragger
listType="image"
action="http://localhost:8080/file"
accept="image/png, image/jpg, image/jpeg, image/gif, image/bmp"
onDragOver={onDragOver}
onDrop={onDrop}
/>
官方文档地址:https://ice.work/component/upload#拖拽上传
springboot 后端代码(就做个简单的测试,所以写在一块了
//跨域配置(注意,一定要 配置详细的地址,不然后报错的
@CrossOrigin(origins = "http://localhost:4444")
@Controller
public class xxxxxxxxxxxxxx {
@ResponseBody
@RequestMapping(path = "file", method = RequestMethod.POST)
public String file(@RequestParam("file") MultipartFile file) {
System.out.println("---------------------------------->"+file.getSize());
return "{\"success\":true,\"message\":\"上传成功\",\"url\":\"http://dummyimage.com/600x600/79e4f2&text=test\",\"imgURL\":\"http://dummyimage.com/600x600/f2dc79&text=test\",\"downloadURL\":\"http://dummyimage.com/600x600/b879f2&text=test\"}";
}
}
Access-Control-Allow-Credentials
头指定了当浏览器的credentials设置为true时是否允许浏览器读取response的内容。当用在对preflight预检测请求的响应中时,它指定了实际的请求是否可以使用credentials。请注意:简单
GET 请求不会被预检;如果对此类请求的响应中不包含该字段,这个响应将被忽略掉,并且浏览器也不会将相应内容返回给网页。
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
@Configuration
public class WebMvcConfig extends WebMvcConfigurationSupport {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedOrigins("*")
.allowedMethods("POST", "GET", "PUT", "OPTIONS", "DELETE")
.maxAge(3600)
.allowCredentials(true);
}
}