1 RequestParams params = new RequestParams(); 2 params.add("ordernum",ordernum); 3 params.add("username",username); 4 for(int i=0; i<filesList.size();i++){ 5 try { 6 params.put("images[" + i + "]",filesList.get(i),"application/octet-stream"); 7 } catch (FileNotFoundException e) { 8 e.printStackTrace(); 9 } 10 Log.i(MyConfig.TagPic,"打印准备上传的图片资料流:"+filesList.get(i).getPath()); 11 } 12 MyBaseClient.post(MyConfig.urlDataUpload,params,new AsyncHttpResponseHandler(){ 13 @Override 14 public void onStart() { 15 super.onStart(); 16 } 17 18 @Override 19 public void onProgress(int bytesWritten, int totalSize) { 20 super.onProgress(bytesWritten, totalSize); 21 int count = (int) ((bytesWritten * 1.0 / totalSize) * 100); 22 // 上传进度显示 23 progressBar.setProgress(count); 24 tv_progress.setText("正在上传资料....."+count+"%"); 25 Log.i("上传 Progress>>>>>", "count="+count+"--"+bytesWritten + " / " + totalSize); 26 } 27 28 @Override 29 public void onSuccess(int statusCode, String content) { 30 super.onSuccess(statusCode, content); 31 Log.i("main","成功了"); 32 mDialog.dismiss(); 33 34 } 35 36 @Override 37 public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) { 38 super.onFailure(statusCode, headers, responseBody, error); 39 mDialog.dismiss(); 40 } 41 });
最近使用asynchttpClient提交表单上传图片,发现存在上传多张图片,会少上传一两张的情况,貌似是这框架的BUG;
改用xUtil可以成功上传;
1 String ordernum = model.getOrdernum(); 2 String username = model.getUsername(); 3 RequestParams params = new RequestParams(); 4 params.addBodyParameter("ordernum",ordernum); 5 params.addBodyParameter("username",username); 6 for(int i=0; i<filesList.size();i++){ 7 params.addBodyParameter("images[" + i + "]",filesList.get(i)); 8 Log.i(MyConfig.TagPic,"打印准备上传的图片资料流:"+filesList.get(i).getPath()); 9 } 10 HttpUtils http = new HttpUtils(); 11 http.send(HttpRequest.HttpMethod.POST, MyConfig.urlDataUpload, params, 12 new RequestCallBack<String>() { 13 14 @Override 15 public void onSuccess(ResponseInfo<String> responseInfo) { 16 Log.i("main","当前结果:"+responseInfo.result); 17 mDialog.dismiss(); 18 netTask(); 19 isUpload = false; 20 hasCompress = false; 21 mSelectPath.clear(); 22 filesList.clear(); 23 mHashMapCompress.clear(); 24 adapter.notifyDataSetChanged(); 25 tv_right_submit.setEnabled(true); 26 //删除手机下面的小图片 27 if(!MyConfig.OpenDebugging){ 28 FileUtil.deleteFileDir(MyConfig.PicFileSmallDir,false); 29 } 30 } 31 32 @Override 33 public void onLoading(long total, long current, boolean isUploading) { 34 super.onLoading(total, current, isUploading); 35 int count = (int) ((current * 1.0 / total) * 100); 36 // 上传进度显示 37 progressBar.setProgress(count); 38 tv_progress.setText("正在上传资料....."+count+"%"); 39 Log.i("上传 Progress>>>>>", "count="+count+"--"+current + " / " + total); 40 } 41 42 @Override 43 public void onFailure(HttpException e, String s) { 44 mDialog.dismiss(); 45 } 46 });
附:thinkphp接口:
1 //上传资料 2 public function upload() 3 { 4 $config = array( 5 //'rootPath' => 'E:/phpStudy/www/yne_siteM/uploads/scan/'.'file/', 6 'rootPath' => 'D:/www/yne_siteM/uploads/scan/'.'file/', 7 ); 8 $upload = new \Think\Upload($config); 9 // 实例化上传类 10 $upload->maxSize = 3145728 ;// 设置附件上传大小 11 $upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型 12 $upload->savePath = 'imgs'; // 设置附件上传(子)目录 13 // 上传文件 14 $info = $upload->upload(); 15 if(!$info) {// 上传错误提示错误信息 16 $this->error($upload->getError()); 17 }else{ 18 // 上传成功 获取上传文件信息 19 foreach($info as $file){ 20 echo $file['savepath'].$file['savename']; 21 22 $db = M('order'); 23 $data['materialurl'] = 'uploads/scan/file/'.$file['savepath'].$file['savename']; 24 $where['username'] = $_POST['username']; 25 $where['ordernum'] = $_POST['ordernum']; 26 $da = $db->field('materialurl')->where($where)->select(); 27 if($da){ 28 $datas['materialurl'] = $da[0]['materialurl'].'|'.$data['materialurl']; 29 $tis = $db->where($where)->save($datas); 30 }else{ 31 $tis = $db->where($where)->save($data); 32 } 33 } 34 } 35 /*$db1 = M('order'); 36 $d = $db1->field('materialurl')->where($where)->select();*/ 37 if($tis){ 38 $response['status'] = 'Y'; 39 $response['msg'] = '成功'; 40 $response['data'] = $tis; 41 echo json_encode($response); 42 }else{ 43 $response['status'] = 'N'; 44 $response['msg'] = '失败'; 45 echo json_encode($response); 46 } 47 // $this->ajaxReturn(true); 48 49 // "file"名字必须和iOS客户端上传的name一致 50 /*if (($_FILES["file"]["type"] == "image/gif") 51 || ($_FILES["file"]["type"] == "image/jpeg") 52 || ($_FILES["file"]["type"] == "image/png") 53 || ($_FILES["file"]["type"] == "imagepeg")) 54 { 55 if ($_FILES["file"]["error"] > 0) { 56 echo $_FILES["file"]["error"]; // 错误代码 57 } else { 58 $fillname = $_FILES['file']['name']; // 得到文件全名 59 $dotArray = explode('.', $fillname); // 以.分割字符串,得到数组 60 $type = end($dotArray); // 得到最后一个元素:文件后缀 61 62 $path = "E:/phpStudy/www/yne_siteM/uploads/scan/".md5(uniqid(rand())).'.'.$type; // 产生随机唯一的名字 63 64 move_uploaded_file( // 从临时目录复制到目标目录 65 $_FILES["file"]["tmp_name"],$path); 66 echo "成功"; 67 } 68 } else { 69 echo "文件类型不正确"; 70 }*/ 71 }