【发布时间】:2016-05-26 13:07:56
【问题描述】:
我尝试将文件夹的内容上传到服务器。但我做不到,所以我做了一个例子,你必须选择每个文件。
控制器:
@Controller
public class FileUploadController {
@RequestMapping(value = "/save", method = RequestMethod.POST)
public String save(@ModelAttribute("uploadForm") FileUploadForm uploadForm,Model map) {}
}
和 jsp 文件
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<html>
<head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">
<title> - Upload</title>
<script
src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<script>
$(document).ready(function() {
//add more file components if Add is clicked
$('#addFile').click(function() {
var fileIndex = $('#fileTable tr').children().length;
$('#fileTable').append(
'<tr><td>'+
' <input type="file" name="files['+ fileIndex +']" />'+
'</td></tr>');
});
});
</script>
</head>
<body>
<div align="center" class="jumbotron">
<h1>dd</h1>
<form:form method="post" action="save.html"
modelAttribute="uploadForm" enctype="multipart/form-data">
<p>Dateien auswählen zum uploaden</p>
<input class="btn btn-success" id="addFile" type="button" value="Datei hinzufügen" />
<table align="center" class="table table-striped" id="fileTable">
<tr>
<td><input name="files[0]" type="file" /></td>
</tr>
<tr>
<td><input name="files[1]" type="file" /></td>
</tr>
</table>
<br/><input class="btn btn-primary" type="submit" value="Upload" />
</form:form>
</div>
</body>
</html>
效果很好。我发现了一些示例,我可以一次选择多个文件(只需添加多个),但无法上传它们。我遇到了一些麻烦才能让这个工作。我很惊讶做一件如此“简单”的事情是如此“困难”。我不知道我是否必须更改我的控制器文件或只更改我的 jsp 文件。我希望有人可以解释我这是如何工作的。我是新人,所以请像对一个小傻孩子一样跟我说话。 问候 山姆
【问题讨论】:
标签: javascript java spring model-view-controller file-upload