cordova-plugin-file-transfer 文件上传

转载自:https://blog.csdn.net/u014505277/article/details/51851495

使用cordova插件上传文件,大家会遇到后台怎么接收数据的问题,这篇文章会前后端结合讲述cordova-plugin-file-transfer的使用;希望能帮助到读者。

1、前端js的调用cordova插件

[javascript]   view plain  copy
  1. function onDeviceReady(fileURL) {  
  2.         var win = function (r) {  
  3.             console.log("Code = " + r.responseCode);  
  4.             console.log("Response = " + r.response);  
  5.             console.log("Sent = " + r.bytesSent);  
  6.         }  
  7.   
  8.         var fail = function (error) {  
  9.             alert("An error has occurred: Code = " + error.code);  
  10.             console.log("upload error source " + error.source);  
  11.             console.log("upload error target " + error.target);  
  12.         }  
  13.   
  14.         var options = new FileUploadOptions();  
  15.         options.fileKey = "file";   // 表单元素的名称。同input标签中的name属性。  
  16.         options.fileName = fileURL.substr(fileURL.lastIndexOf('/') + 1); // 文件名称  
  17.         alert("文件名称"+options.fileName);  
  18.         //options.mimeType = "text/plain";   // 文件类型,默认是image/jpeg   
  19.   
  20.         var params = {};  
  21.         params.value1 = "test";  
  22.         params.value2 = "param";  
  23.   
  24.         options.params = params;  
  25.   
  26.         var ft = new FileTransfer();  
  27.           
  28.         // fileURL 待上传文件路径  
  29.         // encodeURI("***") 上传服务器路径  
  30.         ft.upload(fileURL, encodeURI("http://172.16.5.186:8081/Demo/demoManager/fileAdd.vc"), win, fail, options);  
  31.     }  


2、后台服务接收上传文件

[java]   view plain  copy
  1. @RequestMapping(value = "/fileAdd", method = { RequestMethod.POST,RequestMethod.GET })  
  2.     public String usersInsert(  
  3.             @RequestParam(value="file",required=false) MultipartFile file1,  
  4.             HttpServletRequest request, HttpServletResponse response,  
  5.             ModelMap model) {  
  6.         Map<String, Object> result = new HashMap<String, Object>(); // 用于保存 返回信息  
  7.         // 新建list存放MultiparFile,便于遍历操作数据库。  
  8.         ArrayList<MultipartFile> muList = new ArrayList<MultipartFile>();  
  9.         muList.add(file1);  
  10.         }  
  11.   
  12.         if(muList.size()<1){  
  13.             // copy文件过程失败  
  14.             return null;  
  15.         }  
  16.         for (int i = 0; i < muList.size(); i++) {  
  17.             MultipartFile collectFile = muList.get(i);  
  18.             String pedestalName = collectFile.getOriginalFilename(); // 获取文件名称  
  19.               
  20.             String fileSavePath = "D:\\"+pedestalName; // 文件保存路径  
  21.   
  22.             try {  
  23.                 collectFile.transferTo(new File(fileSavePath)); // 将文件保存到临时文件夹中  
  24.             } catch (IOException e1) {  
  25.                 // copy文件过程失败  
  26.                 errorMessage("文件上传失败,请重新上传文件!", result, response); // 提示信息  
  27.                 return null;  
  28.             }  
  29.         }  
  30.         return null;  
  31.     } 

猜你喜欢

转载自blog.csdn.net/menghuannvxia/article/details/80653320