ajaxFileUpload可以同时多个input file控件一起上传吗
若以下回答无法解决问题,邀请你更新回答
1个回答
2016-10-12 · 百度知道合伙人官方认证企业
兄弟连教育
兄弟连教育成立于2006年,11年来专注IT职业教育,是国内专业的IT技术培训学校。2016年成功挂牌新三板(股票代码:839467)市值过亿。开设专注程序员培训专注php、Java、UI、云计算、Python、HTML5、
向TA提问
关注
展开全部
——————jsp页面代码————————————————————
<form action="doUpload" method="post" enctype="multipart/form-data">
<table id="mytb">
<tr>
<td><input type="file" name="upload" id="upload" value=""></td>
<td><input type="button" name="del" id="del" value="删除"
onclick="delRow(this)" style="display: display;"></td>
</tr>
</table>
<input type="button" name="add" id="add" value="继续添加"
onclick="addRow()"> <input type="submit" value="上传">
</form>
————————js代码————————————————————————
<script type="text/javascript">
function addRow() {
var table = document.getElementById("mytb");//获取表格对象
if (table.rows.length > 9) {
alert("不能再加了,最多上传10个附件");
} else {
var tr = table.insertRow();//为表格增加行
var td1 = tr.insertCell();//为行增加单元格
var td2 = tr.insertCell();
td1.innerHTML = "<input name='upload' type='file' id='upload'/>";//为单元格增加内容
td2.innerHTML = "<input type='button' name='del' id='del' value='删除' onclick='delRow(this)'>";
}
}
function delRow(obj) { //参数为按钮对象
var row = obj.parentNode.parentNode; //按钮所在行
var table = row.parentNode; //按钮所在的表格
var rowIndex = row.rowIndex; //按钮所在的行下标
if (table.rows.length == 1) {
alert("不能再删了,至少保留一行");
} else {
table.deleteRow(rowIndex); //删除当前行
}
}
</script>
——————————————action类代码——————————————
package action;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import org.apache.commons.io.IOUtils;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class UploadAction2 extends ActionSupport {
private static final long serialVersionUID = 1L;
private List<File> upload;
private List<String> uploadContentType;
private List<String> uploadFileName;
public String doUpload() throws IOException{
for(int i=0;i<uploadFileName.size();i++){
String savePath=ServletActionContext.getServletContext().getRealPath("/upload/"+this.uploadFileName.get(i));
FileInputStream fis = new FileInputStream(upload.get(i));
FileOutputStream fos = new FileOutputStream(savePath);
System.out.println(upload.get(i));
System.out.println(uploadContentType.get(i));
System.out.println(uploadFileName.get(i));
System.out.println(savePath);
IOUtils.copy(fis, fos);
fos.flush();
fos.close();
fis.close();
}return SUCCESS;
}
public List<File> getUpload() {
return upload;
}
public void setUpload(List<File> upload) {
this.upload = upload;
}
public List<String> getUploadContentType() {
return uploadContentType;
<form action="doUpload" method="post" enctype="multipart/form-data">
<table id="mytb">
<tr>
<td><input type="file" name="upload" id="upload" value=""></td>
<td><input type="button" name="del" id="del" value="删除"
onclick="delRow(this)" style="display: display;"></td>
</tr>
</table>
<input type="button" name="add" id="add" value="继续添加"
onclick="addRow()"> <input type="submit" value="上传">
</form>
————————js代码————————————————————————
<script type="text/javascript">
function addRow() {
var table = document.getElementById("mytb");//获取表格对象
if (table.rows.length > 9) {
alert("不能再加了,最多上传10个附件");
} else {
var tr = table.insertRow();//为表格增加行
var td1 = tr.insertCell();//为行增加单元格
var td2 = tr.insertCell();
td1.innerHTML = "<input name='upload' type='file' id='upload'/>";//为单元格增加内容
td2.innerHTML = "<input type='button' name='del' id='del' value='删除' onclick='delRow(this)'>";
}
}
function delRow(obj) { //参数为按钮对象
var row = obj.parentNode.parentNode; //按钮所在行
var table = row.parentNode; //按钮所在的表格
var rowIndex = row.rowIndex; //按钮所在的行下标
if (table.rows.length == 1) {
alert("不能再删了,至少保留一行");
} else {
table.deleteRow(rowIndex); //删除当前行
}
}
</script>
——————————————action类代码——————————————
package action;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import org.apache.commons.io.IOUtils;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class UploadAction2 extends ActionSupport {
private static final long serialVersionUID = 1L;
private List<File> upload;
private List<String> uploadContentType;
private List<String> uploadFileName;
public String doUpload() throws IOException{
for(int i=0;i<uploadFileName.size();i++){
String savePath=ServletActionContext.getServletContext().getRealPath("/upload/"+this.uploadFileName.get(i));
FileInputStream fis = new FileInputStream(upload.get(i));
FileOutputStream fos = new FileOutputStream(savePath);
System.out.println(upload.get(i));
System.out.println(uploadContentType.get(i));
System.out.println(uploadFileName.get(i));
System.out.println(savePath);
IOUtils.copy(fis, fos);
fos.flush();
fos.close();
fis.close();
}return SUCCESS;
}
public List<File> getUpload() {
return upload;
}
public void setUpload(List<File> upload) {
this.upload = upload;
}
public List<String> getUploadContentType() {
return uploadContentType;
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询