【发布时间】:2017-10-29 22:53:25
【问题描述】:
尝试修改以下代码: http://blog.netgloo.com/2015/02/08/spring-boot-file-upload-with-ajax/
只需一个文件输入就可以很好地工作(在多个项目中使用没有问题)。
我的要求是创建一个表格,每行都有一个文件输入。
我在输入文件的 id 中添加了一个代码。稍后我将在我的控制器中使用该代码。
我创建了表(好的)
<div class="table-responsive" th:if="${not #lists.isEmpty(objects)}">
<table id="d" class="table table-hover table-sm table-responsive ">
<thead class="thead-inverse">
<tr>
<th>Code</th>
<th>Name</th>
<th>Address</th>
<th>Telephone</th>
<th>File Upload</th>
</tr>
</thead>
<tbody>
<tr th:each="object: ${objects}">
<td th:text="${object.code}"></td>
<td th:text="${object.name}"></td>
<td th:text="${object.addres}"></td>
<td th:text="${object.telephone}"></td>
<td>
<form th:id="'uploadfileform-' + ${object.code}">
<div class="form-group">
<label class="custom-file">
<input type="file" th:id="'uploadfileinput-' + ${object.code}"
name="uploadfile" accept="*" aria-describedby="fileHelp"
class="custom-file-input"/>
<span class="custom-file-control"></span>
</label>
</div>
</form>
</td>
</tr>
</tbody>
</table>
</div>
我添加到表单的 id 并且 input 已分配值。
我的 jquery
第 1 部分(好的)
$(document).ready(function () {
$('input[type="file"]').change(function (e) {
var id = $(this).attr('id');
var res = id.split("-");
// I pass the code
uploadFile(res[1]);
});
});
第 2 部分(失败)
function uploadFile(id) {
$.ajax({
url: "/uploadFile",
type: "POST",
data: new FormData($("#uploadfileform" + id)[0]),
enctype: 'multipart/form-data',
processData: false,
contentType: false,
cache: false,
success: function () {
// Handle upload success
$("#upload-file-message").text(
"File succesfully uploaded");
alert("File succesfully uploaded");
},
error: function () {
// Handle upload error
$("#upload-file-message")
.text(
"File not uploaded (perhaps it's too much big)");
alert("File not uploaded (perhaps it's too much big)");
}
});
}
我相信它失败了:
data : new FormData($("#uploadfileform"+id)[0]),
但我看不到调试方法。
这是控制器的一部分,与博客中的完全一样:
@RequestMapping(value = "/uploadFile", method = RequestMethod.POST)
@ResponseBody
public ResponseEntity<?> uploadFile(@RequestParam("uploadfile") MultipartFile uploadfile) {
更新 1
将数据更改为:
data : new FormData($('input[type="file"]')[0]),
在这两种情况下我都会得到
POST http://localhost:8080/uploadFile 400 ()
谢谢
更新 2
$(document).ready(function () {
//http://blog.netgloo.com/2015/02/08/spring-boot-file-upload-with-ajax/
$('input[type="file"]').change(function (e) {
//var file = $('input[type="file"][0]');
var file = $('input[type="file"]')[0];
alert("File name: " + file.fileName);
alert("File size: " + file.fileSize);
});
});
我没有定义,所以我认为这种代码不适合这种情况。
【问题讨论】:
-
您好,我在这行
new FormData($("#uploadfileform"+id)[0])中看到一个可能的错误:它应该是"#uploadfileform-"+id(在id 前有一个连字符) -
已更改,是错字。数据:新 FormData($("#uploadfileform-"+id)[0]),我从错误函数和控制台中收到消息:.w.s.m.s.DefaultHandlerExceptionResolver:已解决由处理程序执行引起的异常:org.springframework.web.multipart。 support.MissingServletRequestPartException:所需的请求部分“上传文件”不存在
-
@davisoski,您能否检查生成的 html 是否符合预期,并在 Chrome 网络选项卡中检查 POST 数据是否正常?
-
@TarunLalwani 添加了一些 cmets
-
@davisoski,你能提供一个 git repo 来调试吗?这可能需要调试
标签: java jquery html spring thymeleaf