@Controller
public class PreviewController {
@GetMapping("/upload")
public String uploadFile() {
return "recipient";
}
@PostMapping("/preview")
@ResponseBody
public Object previewFile(@RequestParam("uploadFile") MultipartFile file) throws IOException {
Map map = new HashMap();
if (file.getSize() == 0) {
map.put("Message", "file does not exist !");
return map;
} else {
map.put("Message", "file uploaded success");
return map;
}
}
}
和我的ajax
<script>
$("button.searchBtn").click(function () {
var formData = new FormData($("#form-add")[0]);
$.ajax({
url: "/preview",
type: "post",
data: formData,
processData: false,
contentType: false,
success: function (data) {
console.log(data)
}
})
})
</script>
我的html代码
<form action="" id="form-add">
<div class="col-xs-9 p0">
<div class="col-xs-9 p0">
<span class="ui-uploadFileName">upload file</span>
</div>
<div class="col-xs-3">
<label class="ui-upload">upload<input style="display: none;" type="file" name="uploadFile"></label>
</div>
</div>
<script>
$(function () {
$("input[type='file']").click(function () {
$(this).on('change', function (e) {
var name = e.currentTarget.files[0].name;
$(this).parent().parent().siblings("div").find(".ui-uploadFileName").text(name)
});
});
})
</script>
<div class="col-xs-2">
<button class="searchBtn" type="button">preview</button>
</div>
</form>
我使用ajax上传excel文件。
我想再次存储上传的excel。如何将文件制作为excel。
我认为我的代码不是很好,如何优化此代码或更好的方法?