互联网技术 · 2024年2月22日

使用ajax实现SpringBoot的图片上传功能

这篇文章主要介绍了Spring Boot利用 ajax实现上传图片功能,本文图文实例相结合,给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下

基于SpringBoot利用ajax实现上传图片功能

SpringBoot重写addResourceHandlers映射文件路径

@Override
 public void addResourceHandlers(ResourceHandlerRegistry registry) {
   registry.addResourceHandler(“/imctemp-rainy/**”).addResourceLocations(“file:D:/E/”);
 }

设置静态资源路径

2. 表单 前端 页面

<input type=”file” name=”file” id=”file”>
<p id=”url”><img src=”” width=200></p>
<input type=”button” id=”button” value=”上传” >
$(function () {
    $(“#button”).click(function () {
      var form = new FormData();
      form.append(“file”, document.getElementById(“file”).files[0]);
       $.ajax({
         url: “/stu/upload”,    //后台url
         data: form,
         cache: false,
         async: false,
         type: “POST”,          //类型,POST或者GET
         dataType: json,       //数据返回类型,可以是xml、json等
         processData: false,
         contentType: false,
         success: function (data) {   //成功,回调函数
           if (data) {
           var pic=”/imctemp-rainy/”+data.fileName;
       &

OpenMagic API

Need more than content? Move into the product flow.

If you are here for model access, pricing, developer docs, or the future API console, the dedicated product path now lives on api.openmagic.ai.

登录免费注册