網站首頁 編程語言 正文
本文實例為大家分享了Axios和Jquery實現文件上傳的具體代碼,供大家參考,具體內容如下
Jquery上傳
jquery文件時,后端好像并沒有經過SpringMVC處理,沒有被封裝成一個MultiPartFIle對象,可通過原生的Servlet API request.getInputStream()獲取。至于為什么沒被SpringMVC封裝成MultipartFile對象目前還沒有研究透徹。可能是請求內容類型沒有設置成 multipart/form-data。下面是jquery上傳文件的代碼示例,文件名,文件大小等參數可通過拼在url后面使用request.getParameter()獲取。
<!DOCTYPE html>
<html lang="en">
<head>
? ? <meta charset="UTF-8">
? ? <title>Title</title>
</head>
<script src="jquery.js"></script>
<script src="axios.js"></script>
<body>
請上傳圖片:<input type="file" name="file" id="file" onchange="fileChange(this)">
<br>
<button onclick="jqueryUpload()">jquery提交</button>
</body>
<script>
? ? var file = undefined
? ? function fileChange(data){
? ? ? ? file = data.files[0]
? ? }
? function jqueryUpload(){
? ? $.ajax({
? ? ? ? url:"/jqueryUpload?filename=test.jpg",
? ? ? ? type:"post",
? ? ? ? data:file,
? ? ? ? contentType:false,
? ? ? ? processData:false,
? ? ? ? success(res){
? ? ? ? ? ? console.log('上傳結果' + res)
? ? ? ? }
? ? })
? }
</script>
</html>
@PostMapping("jqueryUpload")
? ? public String jqueryUpload(HttpServletRequest request, MultipartFile file) throws Exception{
? ? ? ? System.out.println(file);
? ? ? ? String fileDesc = "D:\\2022\\" + request.getParameter("filename");
? ? ? ? FileOutputStream outputStream = new FileOutputStream(fileDesc);
? ? ? ? ServletInputStream inputStream = request.getInputStream();
? ? ? ? byte[] bytes = new ?byte[1024];
? ? ? ? int line = inputStream.read(bytes);
? ? ? ? while (line != -1){
? ? ? ? ? ? outputStream.write(bytes,0,line);
? ? ? ? ? ? line = inputStream.read(bytes);
? ? ? ? }
? ? ? ? inputStream.close();
? ? ? ? outputStream.close();
? ? ? ? return "success";
? ? }
此時后臺打印的multipartfile是null。后續會深入研究·······…
Axios上傳
axios上傳不同于jquery,axios將上傳的二進制數據和其他參數封裝在了FormData對象中,值得注意的是,此時的內容類型要改為multipart/form-data。
<!DOCTYPE html>
<html lang="en">
<head>
? ? <meta charset="UTF-8">
? ? <title>Title</title>
</head>
<script src="jquery.js"></script>
<script src="axios.js"></script>
<body>
請上傳圖片:<input type="file" name="file" id="file" onchange="fileChange(this)">
<br>
<button onclick="jqueryUpload()">jquery提交</button>
<button onclick="axiosUpload()">axios提交</button>
</body>
<script>
? ? var file = undefined
? ? function fileChange(data){
? ? ? ? file = data.files[0]
? ? }
? function axiosUpload(){
? ? var formData = new FormData();
? ? formData.append("file",file);
? ? formData.append("filename","myfile.jpg")
? ? axios.post("/axiosUpload",formData,{
? ? ? ? headers:{ "Content-Type" : "multipart/form-data" }
? ? }).then(res => {
? ? ? ? console.log('上傳結果' + res)
? ? })
? }
</script>
</html>
@PostMapping("axiosUpload")
? ? public String axiosUpload(HttpServletRequest request,MultipartFile file)throws Exception{
? ? ? ? InputStream inputStream = file.getInputStream();
? ? ? ? String fileDesc = "D:\\2022\\" + request.getParameter("filename");
? ? ? ? FileOutputStream os = new FileOutputStream(fileDesc);
? ? ? ? byte[] bytes = new ?byte[1024];
? ? ? ? int line = inputStream.read(bytes);
? ? ? ? while (line != -1){
? ? ? ? ? ? os.write(bytes,0,line);
? ? ? ? ? ? line = inputStream.read(bytes);
? ? ? ? }
? ? ? ? inputStream.close();
? ? ? ? os.close();
? ? ? ? return "success";
? ? }
感覺還是更喜歡axios這種,明確指定了內容類型并且經過了SpringMVC處理。
原文鏈接:https://blog.csdn.net/qq_43750656/article/details/121426546
相關推薦
- 2023-05-15 Bash中test命令的使用_linux shell
- 2022-07-18 CSS基礎語法和盒模型
- 2023-05-29 scipy稀疏數組coo_array的實現_python
- 2022-03-23 C語言實現貪吃蛇小黑窗_C 語言
- 2022-06-01 Python實現xml格式轉txt格式的示例代碼_python
- 2022-05-11 并發編程之JMM模型和并發三大特性
- 2022-09-15 如何使用注解方式實現?Redis?分布式鎖_Redis
- 2022-11-27 Git基礎學習之文件刪除操作命令詳解_相關技巧
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支