欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Bootstrap FileInput實(shí)現(xiàn)圖片上傳功能

 更新時(shí)間:2021年01月28日 15:43:24   作者:RNG-航仔  
這篇文章主要為大家詳細(xì)介紹了Bootstrap FileInput實(shí)現(xiàn)圖片上傳功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了Bootstrap FileInput實(shí)現(xiàn)圖片上傳功能的具體代碼,供大家參考,具體內(nèi)容如下

html代碼:

<div class="form-group">
 <label class="col-sm-2 control-label">圖片</label>
 <div class="col-sm-8">
  <input id="filesInput" type="file" multiple data-browse-on-zone-click="true" class="file-loading" accept="image/*" />
  <input id="resources" name="resources" th:value="${record.picUrls}" type="hidden">//獲取上傳的圖片路徑,$("#filesInput").val()獲取不到,用隱藏的input來(lái)獲取
 </div>
</div>

引入js和css文件

<link href="/ajax/libs/bootstrap-fileinput/fileinput.css" rel="stylesheet" type="text/css"/>
<script th:src="@{/js/jquery.min.js}"></script>
<script th:src="@{/js/bootstrap.min.js}"></script>
<script th:src="@{/ajax/libs/bootstrap-fileinput/fileinput.js}"></script>

js代碼:

var List = new Array();//定義一個(gè)全局變量去接受文件名和id
$(function () {
 var picStr = [
http:...,
http:....
];
var picStrConfig = [
{caption: "11111", width: "120px", fileid:"123456", url: "deleteData", type:"image", key: "1"},
........
];
$('#filesInput').fileinput({
 theme: 'fas',
 language: 'zh',
 uploadUrl: ctx + 'bike/record/uploadData',
 uploadAsync: true, //默認(rèn)異步上傳
 showUpload: true, //是否顯示上傳按鈕
 overwriteInitial: false,
 showRemove : false, //顯示移除按鈕
 // showPreview : true, //是否顯示預(yù)覽
 showCaption: false,//是否顯示標(biāo)題
 browseClass: "btn btn-primary", //按鈕樣式
 dropZoneEnabled: false,//是否顯示拖拽區(qū)域
 maxFileCount: 5, //表示允許同時(shí)上傳的最大文件個(gè)數(shù)
 enctype: 'multipart/form-data',
 validateInitialCount:true,
 layoutTemplates: {actionUpload: ''},
 maxFilesNum: 5,
 fileType: "any",
 allowedPreviewTypes: ['image'],
 previewFileIcon: "<i class='glyphicon glyphicon-king'></i>",
 initialPreviewAsData: true,
 initialPreview: picStr, //初始化回顯圖片路徑
 initialPreviewConfig: picStrConfig //配置預(yù)覽中的一些參數(shù)
 
}).on("fileuploaded", function (event, data, previewId, index) {
 var response = data.response;
 var filePath = data.response.filePath; //文件上傳成功返回的文件名,可返回自定義文件名
 List.push({ FileName: filePath, KeyID: previewId })
 // alert(response.filePath);
 // $("#fileMd5").val(response.fileMd5);
 // $("#version").val(response.newVersionName);
 var resources = $('#resources').val();
 if (!resources){
  $("#resources").val(response.filePath);
 }else{
  $("#resources").val(resources+"^_^"+response.filePath);
 }
 
 
}).on('filepredelete', function(event, data, previewId, index) { //刪除原圖片之前的觸發(fā)動(dòng)作
 
 
}).on('filedeleted',function (event, data, previewId, index) {//刪除原圖片之后的動(dòng)作
 var resources = $("#resources").val();
 var respone = previewId.responseJSON;
 if(respone.code == 0){
  var deleteName = "/"+data;
  if(resources.indexOf("^_^"+deleteName)>-1){
   $("#resources").val("^_^"+resources.replace(deleteName,""));
   resources = $("#resources").val();
  }
  if(resources.indexOf(deleteName+"^_^")>-1){
   $("#resources").val(resources.replace(deleteName+"^_^",""));
   resources = $("#resources").val();
  }
  if(resources.indexOf(deleteName)>-1){
   $("#resources").val(resources.replace(deleteName,""));
   resources = $("#resources").val();
  }
 }
}).on('filepreupload', function(event, data, previewId, index) {
 var form = data.form, files = data.files, extra = data.extra,
  response = data.response, reader = data.reader;
}).on("filesuccessremove", function (event, data, previewId, index) {
 var resources = $("#resources").val();
 for (var i = 0; i < List.length; i++) {
  if (List[i].KeyID == data) {
   if(resources.indexOf("^_^"+List[i].FileName)>-1){
    $("#resources").val("^_^"+resources.replace(List[i].FileName,""));
    resources = $("#resources").val();
   }
   if(resources.indexOf(List[i].FileName+"^_^")>-1){
    $("#resources").val(resources.replace(List[i].FileName+"^_^",""));
    resources = $("#resources").val();
   }
   if(resources.indexOf(List[i].FileName)>-1){
    $("#resources").val(resources.replace(List[i].FileName,""));
    resources = $("#resources").val();
   }
   List[i].KeyID = "1"
  }
 }
});
})

java代碼:

/**
  * 上傳文件
*/
 @RequestMapping("/uploadData")
 @ResponseBody
 public Map<String, Object> uploadData(HttpServletRequest request, HttpServletResponse response) throws Exception{
  request.setCharacterEncoding("UTF-8");
  Map<String, Object> json = new HashMap<String, Object>();
  MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
 
  /** 頁(yè)面控件的文件流* */
  MultipartFile multipartFile = null;
  Map map =multipartRequest.getFileMap();
  for (Iterator i = map.keySet().iterator(); i.hasNext();) {
   Object obj = i.next();
   multipartFile=(MultipartFile) map.get(obj);
 
  }
  /** 獲取文件的后綴* */
  String filename = multipartFile.getOriginalFilename();
 
  InputStream inputStream;
  String path = "";
  String fileMd5 = "";
  try {
   /** 文件上傳到存儲(chǔ)庫(kù)中 **/
   inputStream = multipartFile.getInputStream();
   File tmpFile = File.createTempFile(filename,
   filename.substring(filename.lastIndexOf(".")));
   fileMd5 = Files.hash(tmpFile, Hashing.md5()).toString();
   FileUtils.copyInputStreamToFile(inputStream, tmpFile);
   /** 上傳到 MinIO上 **/
   path = minioFileUtil.uploadCustomize(multipartFile.getInputStream(), filename.substring(filename.lastIndexOf(".")), "",multipartFile.getContentType());
   /** 上傳 到 阿里云oss **/
//   path = AliOSSUtils.getInstance().multpartFileUpload(multipartFile,"bike");
   tmpFile.delete();
 
  } catch (Exception e) {
   e.printStackTrace();
   logger.error("上傳失敗",e);
   json.put("fileMd5", fileMd5);
   json.put("message", "上傳失敗");
   json.put("status", false);
   json.put("filePath", path);
   return json;
  }
  json.put("fileMd5", fileMd5);
  json.put("message", "上傳成功");
  json.put("status", true);
  json.put("filePath", path);
  json.put("key", UUIDGenerator.getUUID());
  return json;
 }
/**
 * 刪除文件文件
 */
@RequestMapping("/edit/deleteData/{id}")
@ResponseBody
@Transactional(rollbackFor = Exception.class)
public AjaxResult deleteData(@PathVariable("id")String id, HttpServletRequest request) throws Exception{
 String key = request.getParameter("key");
 Record record = recordService.getById(id);
 String picUrls = record.getPicUrls();
 String deleteName = "/" + key;
 if (picUrls.indexOf("^_^" + deleteName) > -1) {
  picUrls = "^_^" + picUrls.replace(deleteName, "");
 }
 if (picUrls.indexOf(deleteName + "^_^") > -1) {
  picUrls = picUrls.replace(deleteName + "^_^", "");
 }
 if (picUrls.indexOf(deleteName) > -1) {
  picUrls = picUrls.replace(deleteName, "");
 }
 record.setPicUrls(picUrls);
 if (recordMapper.updatePicsById(record) == 1) { /** 先刪除數(shù)據(jù)庫(kù)中的圖片路徑 再刪除圖片存儲(chǔ)的源文件。**/
  minioUtil.removeObject(bucketName, key);
  return success(key);
 }
 return error();
}

修改 fileInput 源碼:

self._handler($el, 'click', function () {
  if (!self._validateMinCount()) {
   return false;
  }
  self.ajaxAborted = false;
  self._raise('filebeforedelete', [vKey, extraData]);
  //noinspection JSUnresolvedVariable,JSHint
  $.modal.confirm("確定刪除原文件?刪除后不可恢復(fù)",function () { //初始化回顯的圖片在刪除時(shí)彈出提示框確認(rèn)。
  if (self.ajaxAborted instanceof Promise) {
   self.ajaxAborted.then(function (result) {
    if (!result) {
     $.ajax(settings);
    }
   });
  } else {
   if (!self.ajaxAborted) {
    $.ajax(settings);
   }
  }
  })
 });
});

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • IE下js調(diào)試工具Companion.JS

    IE下js調(diào)試工具Companion.JS

    做web開(kāi)發(fā)的朋友都清楚,js程序的調(diào)試是相當(dāng)郁悶的,因?yàn)槭紫冗@種語(yǔ)言語(yǔ)法比較靈活,它是一種弱類型的腳本語(yǔ)言,很多錯(cuò)誤是無(wú)法控制的。
    2010-10-10
  • 使用FormData實(shí)現(xiàn)上傳多個(gè)文件

    使用FormData實(shí)現(xiàn)上傳多個(gè)文件

    這篇文章主要為大家詳細(xì)介紹了使用FormData實(shí)現(xiàn)上傳多個(gè)文件功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-12-12
  • JavaScript中的this妙用實(shí)例分析

    JavaScript中的this妙用實(shí)例分析

    這篇文章主要介紹了JavaScript中的this妙用,結(jié)合實(shí)例形式分析了JavaScript中的this基本功能、用法及操作注意事項(xiàng),需要的朋友可以參考下
    2020-05-05
  • 完美解決IE不支持Data.parse()的問(wèn)題

    完美解決IE不支持Data.parse()的問(wèn)題

    下面小編就為大家?guī)?lái)一篇完美解決IE不支持Data.parse()的問(wèn)題。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起 小編過(guò)來(lái)看看吧
    2016-11-11
  • BootStrap selectpicker后臺(tái)動(dòng)態(tài)綁定數(shù)據(jù)的方法

    BootStrap selectpicker后臺(tái)動(dòng)態(tài)綁定數(shù)據(jù)的方法

    這篇文章主要為大家詳細(xì)介紹了BootStrap selectpicker后臺(tái)動(dòng)態(tài)綁定數(shù)據(jù)的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-07-07
  • 使用TypeScript在接口中定義靜態(tài)方法詳解

    使用TypeScript在接口中定義靜態(tài)方法詳解

    當(dāng)我們談?wù)撁嫦驅(qū)ο缶幊虝r(shí),最難理解的事情之一就是靜態(tài)屬性與實(shí)例屬性的概念,尤其是當(dāng)我們?cè)噲D在靜態(tài)類型的基礎(chǔ)上進(jìn)行動(dòng)態(tài)語(yǔ)言類型化時(shí),在本文中,我將主要介紹一下如何使用TypeScript在接口中定義靜態(tài)方法,需要的朋友可以參考下
    2023-10-10
  • ES6如何將?Set?轉(zhuǎn)化為數(shù)組示例詳解

    ES6如何將?Set?轉(zhuǎn)化為數(shù)組示例詳解

    這篇文章主要為大家介紹了ES6如何將?Set?轉(zhuǎn)化為數(shù)組的方法示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-02-02
  • JavaScript常用內(nèi)置對(duì)象用法分析

    JavaScript常用內(nèi)置對(duì)象用法分析

    這篇文章主要介紹了JavaScript常用內(nèi)置對(duì)象用法,簡(jiǎn)單總結(jié)分析了javascript String對(duì)象、Date對(duì)象、Math類、數(shù)組對(duì)象等常見(jiàn)對(duì)象的相關(guān)功能、方法與使用注意事項(xiàng),需要的朋友可以參考下
    2019-07-07
  • 詳解單頁(yè)面路由工程使用微信分享及二次分享解決方案

    詳解單頁(yè)面路由工程使用微信分享及二次分享解決方案

    這篇文章主要介紹了詳解單頁(yè)面路由工程使用微信分享及二次分享解決方案,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2019-02-02
  • JavaScript驗(yàn)證知識(shí)整理

    JavaScript驗(yàn)證知識(shí)整理

    本文主要介紹了JavaScript驗(yàn)證的相關(guān)知識(shí)整理。具有很好的參考價(jià)值。下面跟著小編一起來(lái)看下吧
    2017-03-03

最新評(píng)論