//数据 data: { imgUrl:[], imgname:[], }, //上传方法 //上传6张', duration: 1500 }) return false; } wx.chooseImage({ count: 1, sizeType: ['compressed'], success: function (res) { var tempFilePaths = res.tempFilePaths; var i = 0; //第几个 var length = res.tempFilePaths.length; //总共个数 var successUp = 0; //成功个数 var failUp = 0; //失败个数 that.uploadImg(tempFilePaths, successUp, failUp, i, length); } }) }, uploadImg: function (tempFilePaths, length) { var that = this; var token = wx.getStorageSync('token'); wx.uploadFile({ url: _data.get_api_imgupload, //仅为示例,非真实的接口地址 filePath: tempFilePaths[i], name: 'file', header: { 'content-type': 'multipart/form-data', 'Authorization': token }, success: function (res) { //console.log(res); var srcArr = that.data.imgUrl; srcArr.push(data.data.src); var imgname = that.data.imgname; imgname.push(data.data.name); that.setData({ imgUrl: srcArr, imgname: imgname }); }, complete: () => { i++; if (i == length) { return; } else { //递归调用uploadDIY函数 if (!that.data.isuploaderror) { this.uploadImg(tempFilePaths, length); } } } }) }, //删除 // 删除第几条数据,第二个数据为要删除的项目数量,通常为1 that.data.imgUrl.splice(Index, 1); that.data.imgname.splice(Index, 1); //渲染数据 that.setData({ imgUrl: that.data.imgUrl, imgname: that.data.imgname }); var url = _data.get_api_imgdel; var data = {'name': name}; _httpHelper.httpPost(url, data, function (res) {}); },
Box-sizing: border-Box; font-weight: 500; line-height: 1.1; color: rgb(51,51,51); margin-top: 1.6em; margin-bottom: 0.6em; font-size: 20px; white-space: normal;">Box-sizing: border-Box; font-weight: 700;">PHP:laravel框架为案例
Box-sizing: border-Box; font-weight: 700;">
/** * 上传图片到本地临时目录 */ public function uploadImgToTemp( Request $request ) { if( $request->file('file') == false ) { responseData(\StatusCode::ERROR,'文件不存在'); } try { $res = $request->file('file')->store('temp', 'temp'); $name = explode('/',$res)[1]; $obj = new \stdClass(); $obj->src = "http://".$_SERVER['HTTP_HOST'].'/temp/'.$name; $obj->name = $name; responseData(\StatusCode::SUCCESS,'上传成功',$obj); } catch (Exception $e) { responseData(\StatusCode::ERROR,'上传失败'); } }/*
@param string $status
@param string $messages
@param string $data
@param string $errorparam
*/
function responseData( $status="", $messages="", $data="", $errorparam="" )
{
$res["status"] = $status;//请求结果的状态
$res["messages"] = $messages;//请求结果的文字描述
$res["data"] = $data;//返回的数据结果
if( $errorparam )
{
$res["errorparam"] = $errorparam; //错误参数对应提示
}
echo json_encode($res);
die;
}