日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

layui多文件上传讲解_Layui 多文件上传 SSH

發布時間:2025/3/12 编程问答 18 豆豆
生活随笔 收集整理的這篇文章主要介紹了 layui多文件上传讲解_Layui 多文件上传 SSH 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

jsp 頁面

pageEncoding="UTF-8"%>

Insert title here

選擇多文件

文件名大小狀態操作

開始上傳

layui.use('upload', function(){

var $ = layui.jquery

,upload = layui.upload;

//多文件列表示例

var demoListView = $('#demoList')

,uploadListIns = upload.render({

elem: '#testList'

,url: '/Lol_uploadFile.action'

,accept: 'file'

,data:{id:123}?? //還可以傳參,參數id=123

,multiple: true

,auto: false

,bindAction: '#testListAction'

,success:function(msg) {

$.each($.parseJSON(msg.jsonData),function(i,item){

if (item.fileName==1) {

alert("ok");

} else {

alert("ok");

}

});

}

,choose: function(obj){

var files = this.files = obj.pushFile(); //將每次選擇的文件追加到文件隊列

//讀取本地文件

obj.preview(function(index, file, result){

var tr = $(['

'

,'

'+ file.name +''

,'

'+ (file.size/1014).toFixed(1) +'kb'

,'

等待上傳'

,'

'

,'重傳'

,'刪除'

,'

'

,'

'].join(''));

//單個重傳

tr.find('.demo-reload').on('click', function(){

obj.upload(index, file);

});

//刪除

tr.find('.demo-delete').on('click', function(){

delete files[index]; //刪除對應的文件

tr.remove();

uploadListIns.config.elem.next()[0].value = ''; //清空 input file 值,以免刪除后出現同名文件不可選

});

demoListView.append(tr);

});

}

,done: function(res, index, upload){

$.each($.parseJSON(res.jsonData),function(i,item){

if(item.success==1) {//上傳成功

var tr = demoListView.find('tr#upload-'+ index)

,tds = tr.children();

tds.eq(2).html('上傳成功');

tds.eq(3).html(''); //清空操作

return delete this.files[index]; //刪除文件隊列已經上傳成功的文件

}

});

this.error(index, upload);

}

,error: function(index, upload){

var tr = demoListView.find('tr#upload-'+ index)

,tds = tr.children();

tds.eq(2).html('上傳失敗');

tds.eq(3).find('.demo-reload').removeClass('layui-hide'); //顯示重傳

}

});

});

action方法

需要添加兩個屬性

List file;

List fileFileName;

//記住要添加對應的get、set

public String updateImg() throws Exception {

JSONArray array = new JSONArray();

JSONObject obj = new JSONObject();

if(UploadFile.uploadFileBase(0, file, fileFileName, UPLOADDIR)) {

BusinessImage bsnImg = new BusinessImage();

bsnImg.setId(Integer.parseInt(imgId));

bsnImg.setImgUrl(UPLOADDIR+"/"+fileFileName.get(0));

businessImageServiceImpI.updateBusinessImageAjax(bsnImg);

obj.put("success", "1");

} else {

obj.put("success", "0");

}

array.put(obj);

jsonData = array.toString();

System.out.println(jsonData);

return SUCCESS;

}

圖片上傳工具類

package com.gxuwz.core.util;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.io.OutputStream;

import java.util.List;

import org.apache.struts2.ServletActionContext;

public class UploadFile {

@SuppressWarnings("deprecation")

public static boolean uploadFileBase(int i,List

file,List fileFileName,String uploadPath) throws Exception

{

try {

InputStream in = new FileInputStream(file.get(i));

String dir = ServletActionContext.getRequest().getRealPath(uploadPath);

File fileLocation = new File(dir);

//此處也可以在應用根目錄手動建立目標上傳目錄

if(!fileLocation.exists()){

boolean isCreated? = fileLocation.mkdir();

if(!isCreated) {

//目標上傳目錄創建失敗,可做其他處理,例如拋出自定義異常等,一般應該不會出現這種情況。

return false;

}

}

String fileName=fileFileName.get(i);

File uploadFile = new File(dir, fileName);

OutputStream out = new FileOutputStream(uploadFile);

byte[] buffer = new byte[1024 * 1024];

int length;

while ((length = in.read(buffer)) > 0) {

out.write(buffer, 0, length);

}

in.close();

out.close();

return true;

} catch (FileNotFoundException ex) {

ex.printStackTrace();

return false;

} catch (IOException ex) {

ex.printStackTrace();

return false;

}

}

}

如果不會配struct.xml 請往下看

jsonData

總結

以上是生活随笔為你收集整理的layui多文件上传讲解_Layui 多文件上传 SSH的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。