使用 input[type=file]上传文件

 1 var $file = $('#file'); 
 2   $('#btn').click(function() { 
 3       var data = new FormData(); 
 4       data.append('file', $file[0].files[0]); 
 5       data.append('foo', 'bar'); 
 6 
 7       var xhr = new XMLHttpRequest(); 
 8       xhr.open('post', '/upload'); 
 9       xhr.onload = function(e) { 
10           alert(e.currentTarget.response); 
11       } 
12       xhr.send(data); 
13   }); 
14   
15   $("#files").change(function(){
16       data = new FormData();
17       data.append('files', $('#files')[0].files[0]);
18       $.ajax({
19           type: "post",
20           url: "/wap/loanapply/uploadFileUrl",
21           processData: false,
22           contentType: false,
23           data: data,
24           success: function(_data){
25               _data = JSON.parse(_data);
26               if (_data.code == "200") {
27                   $scope.$apply(function () {
28                      $scope.formData.attachment_ids_url = _data.msg;
29                       $scope.up_success = true;
30                   });
31               } else {
32                   $layer.alert(_data.msg, 1000)
33               }
34           }
35       });
36   });

猜你喜欢

转载自www.cnblogs.com/souleigh-hong/p/9056032.html