您的位置:首页 > 理论基础 > 计算机网络

asynchttpClient框架关于多文件批量上传的问题,改用xUtil

2016-07-22 12:53 881 查看
RequestParams params = new RequestParams();
params.add("ordernum",ordernum);
params.add("username",username);
for(int i=0; i<filesList.size();i++){
try {
params.put("images[" + i + "]",filesList.get(i),"application/octet-stream");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
Log.i(MyConfig.TagPic,"打印准备上传的图片资料流:"+filesList.get(i).getPath());
}
MyBaseClient.post(MyConfig.urlDataUpload,params,new AsyncHttpResponseHandler(){
@Override
public void onStart() {
super.onStart();
}

@Override
public void onProgress(int bytesWritten, int totalSize) {
super.onProgress(bytesWritten, totalSize);
int count = (int) ((bytesWritten * 1.0 / totalSize) * 100);
// 上传进度显示
progressBar.setProgress(count);
tv_progress.setText("正在上传资料....."+count+"%");
Log.i("上传 Progress>>>>>", "count="+count+"--"+bytesWritten + " / " + totalSize);
}

@Override
public void onSuccess(int statusCode, String content) {
super.onSuccess(statusCode, content);
Log.i("main","成功了");
mDialog.dismiss();

}

@Override
public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
super.onFailure(statusCode, headers, responseBody, error);
mDialog.dismiss();
}
});


最近使用asynchttpClient提交表单上传图片,发现存在上传多张图片,会少上传一两张的情况,貌似是这框架的BUG;

改用xUtil可以成功上传;

String ordernum = model.getOrdernum();
String username = model.getUsername();
RequestParams params = new RequestParams();
params.addBodyParameter("ordernum",ordernum);
params.addBodyParameter("username",username);
for(int i=0; i<filesList.size();i++){
params.addBodyParameter("images[" + i + "]",filesList.get(i));
Log.i(MyConfig.TagPic,"打印准备上传的图片资料流:"+filesList.get(i).getPath());
}
HttpUtils http = new HttpUtils();
http.send(HttpRequest.HttpMethod.POST, MyConfig.urlDataUpload, params,
new RequestCallBack<String>() {

@Override
public void onSuccess(ResponseInfo<String> responseInfo) {
Log.i("main","当前结果:"+responseInfo.result);
mDialog.dismiss();
netTask();
isUpload = false;
hasCompress = false;
mSelectPath.clear();
filesList.clear();
mHashMapCompress.clear();
adapter.notifyDataSetChanged();
tv_right_submit.setEnabled(true);
//删除手机下面的小图片
if(!MyConfig.OpenDebugging){
FileUtil.deleteFileDir(MyConfig.PicFileSmallDir,false);
}
}

@Override
public void onLoading(long total, long current, boolean isUploading) {
super.onLoading(total, current, isUploading);
int count = (int) ((current * 1.0 / total) * 100);
// 上传进度显示
progressBar.setProgress(count);
tv_progress.setText("正在上传资料....."+count+"%");
Log.i("上传 Progress>>>>>", "count="+count+"--"+current + " / " + total);
}

@Override
public void onFailure(HttpException e, String s) {
mDialog.dismiss();
}
});


附:thinkphp接口:

//上传资料
public function upload()
{
$config = array(
//'rootPath' => 'E:/phpStudy/www/yne_siteM/uploads/scan/'.'file/',
'rootPath' => 'D:/www/yne_siteM/uploads/scan/'.'file/',
);
$upload = new \Think\Upload($config);
// 实例化上传类
$upload->maxSize   =     3145728 ;// 设置附件上传大小
$upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
$upload->savePath  =     'imgs'; // 设置附件上传(子)目录
// 上传文件
$info = $upload->upload();
if(!$info) {// 上传错误提示错误信息
$this->error($upload->getError());
}else{
// 上传成功 获取上传文件信息
foreach($info as $file){
echo $file['savepath'].$file['savename'];

$db = M('order');
$data['materialurl'] = 'uploads/scan/file/'.$file['savepath'].$file['savename'];
$where['username'] = $_POST['username'];
$where['ordernum'] = $_POST['ordernum'];
$da = $db->field('materialurl')->where($where)->select();
if($da){
$datas['materialurl'] = $da[0]['materialurl'].'|'.$data['materialurl'];
$tis = $db->where($where)->save($datas);
}else{
$tis = $db->where($where)->save($data);
}
}
}
/*$db1 = M('order');
$d = $db1->field('materialurl')->where($where)->select();*/
if($tis){
$response['status'] = 'Y';
$response['msg'] = '成功';
$response['data'] = $tis;
echo json_encode($response);
}else{
$response['status'] = 'N';
$response['msg'] = '失败';
echo json_encode($response);
}
//    $this->ajaxReturn(true);

// "file"名字必须和iOS客户端上传的name一致
/*if (($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/png")
|| ($_FILES["file"]["type"] == "imagepeg"))
{
if ($_FILES["file"]["error"] > 0) {
echo $_FILES["file"]["error"]; // 错误代码
} else {
$fillname = $_FILES['file']['name']; // 得到文件全名
$dotArray = explode('.', $fillname); // 以.分割字符串,得到数组
$type = end($dotArray); // 得到最后一个元素:文件后缀

$path = "E:/phpStudy/www/yne_siteM/uploads/scan/".md5(uniqid(rand())).'.'.$type; // 产生随机唯一的名字

move_uploaded_file( // 从临时目录复制到目标目录
$_FILES["file"]["tmp_name"],$path);
echo "成功";
}
} else {
echo "文件类型不正确";
}*/
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: