欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Bootstrap Fileinput 4.4.7文件上传实例详解

程序员文章站 2023-02-08 17:37:02
本实例所做功能为发送带附件邮件,可以上传多个附件,操作为选择一个附件以后自动上传,然后继续选择附件,填写完表单其他信息,点击保存发送带附件邮件。 html标签...

本实例所做功能为发送带附件邮件,可以上传多个附件,操作为选择一个附件以后自动上传,然后继续选择附件,填写完表单其他信息,点击保存发送带附件邮件。

html标签

<input id="fileupload" type="file" name="file" data-show-preview="true" multiple/>

js初始化,设置全局文件名参数

var filename = [];
function initfileinput(id, url) {
    $("#" + id).fileinput({
      language: 'zh', 
      uploadasync:false,
      uploadurl:url,
      browseclass: "btn btn-secondary",
      textencoding:"utf-8",
      showupload: false,
      showpreview :true,
      dropzoneenabled: false,
      maxfilecount:5,
      fileactionsettings:{
        showupload: true
      },
      enctype:'multipart/form-data',
      msgfilestoomany: "选择上传的文件数量({n}) 超过允许的最大数值{m}!",
    }).on("filebatchselected", function(event, files) {
      $("#fileupload").fileinput("upload");
    }).on("filebatchuploadsuccess", function (event, data, previewid, index){
      if(data.response.success == true)
      {
        filename.push(data.response.filename);
      }else{
        alert("上传失败!");
      }
      $("#fileupload").fileinput("clear");
      $("#fileupload").fileinput("reset");
    }).on('fileerror', function(event, data, msg) {
       alert(msg);
    });
  }

java后台上传文件代码

@requestmapping(value="/fileupload")
  @responsebody
  public map<string, object> fileupload(httpservletrequest request, httpservletresponse response) {
    resourcebundle bundle = propertyresourcebundle.getbundle("application");
    multiparthttpservletrequest multipartrequest = (multiparthttpservletrequest)request;
    map<string,multipartfile> filemap = multipartrequest.getfilemap();
    string rootpath = bundle.getstring("uploadurl");
    string filepath = rootpath;
    map<string, object> map = new hashmap<>();
    map = uploadfiles(filepath,filemap);
    return map;
  }

实际上传操作,返回上传操作经过处理的文件名,保证服务器端文件名唯一

public map<string, object> uploadfiles(string savepath,map<string,multipartfile> filemap){
    map<string, object> map = new hashmap<>();
    try {
      string filename = "";
      if(filemap!=null){
        for(map.entry<string, multipartfile> entity:filemap.entryset()){
          multipartfile f = entity.getvalue();
          if(f != null && !f.isempty()){
            string uuid = uuid.randomuuid().tostring();
            filename = uuid + "#" + f.getoriginalfilename();
            file newfile = new file(savepath + "/" + filename); 
            f.transferto(newfile);
          }
        }
      }
      map.put("success", true);
      map.put("filename", filename);
      return map;
    }catch (exception e) {
      map.put("success", false);
      return map;
   }
}

ajax提交其他表单参数和所传附件文件名集合

$.ajax({
      type: "post",
      url: 所需要请求地址,
      datatype: "json",
      traditional:true,
      data: {
        service:$("#service").select2('val').replace("all",""),
        starttime:$("#start").val(),
        endtime:$("#end").val(),
        reason:$("#reason").val(),
        filename:json.stringify(filename),
        outteremail:isoutteremail,
        inneremail:isinneremail,
        issendemail:issendemail,
        subservice:$("#subservice").select2('val'),
        runningstatus:$("#runningstatus").select2('val')
      },
      success: function(data){
        $("#loadingmodal").modal("hide");
        filename.splice(0,filename.length);
        alert(data.msg);
        if(data.success) {
          location.href = "revision";
        }
      },
      error:function(xhr) {
        $("#loadingmodal").modal("hide");
        alert("保存失败");
      }
    });

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。