向服务器上传图片和一些数据信息

有时候,项目中需要向服务器不单单是上传图片或者数据信息,要想将图片和一些数据信息一起上传到服务器,用AFNetWorking就可以实现:

//向服务器上传数据
-(void)importDate:(UIButton *)button {


    if (_footerView.reason.text.length != 0) {
        [self showHudInView:self.view hint:nil];
    //要发送给服务器的数据类信息拼接在字典里即可
        NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:_orderId ,@"orderId",_productId,@"productId",_footerView.amountLabel.text,@"count",_footerView.reason.text,@"remark", nil];
        NSLog( @"申请退货的参数  %@",dic);
        AFHTTPRequestOperationManager* mgr = [AFHTTPRequestOperationManager manager];


        //发送请求  上传图片给服务器
        [mgr POST:E_returnGoodsDetailURL parameters:dic constructingBodyWithBlock:^(id<AFMultipartFormData> totalformData) {
            //_imageArr是图片数组(一般是已经转化为NSData类型的图片的数组
         一般方法为:   NSData *imageData = UIImageJPEGRepresentation(_footerView.smallImage.image, 0.5);
            )
            for (int i = 0; i<_imageArr.count; i++) {

                [totalformData appendPartWithFileData:[_imageArr objectAtIndex:i] name:[NSString stringWithFormat:@"image%i",i] fileName:[NSString stringWithFormat:@"image%i.jpg",i] mimeType:@"image/jpeg"];
            }


        } success:^(AFHTTPRequestOperation *operation, id responseObject) {
            [self hideHud];
            //成功回调
            NSLog(@"成功回调====%@",responseObject);
            if ([[NSString stringWithFormat:@"%@",[responseObject objectForKey:@"success"]] isEqualToString:@"1"]) {
                [self weiXinPayShowInfoViewWith:@"提交成功" ];
                [self.navigationController popViewControllerAnimated:YES];
            }else
            {
                [self weiXinPayShowInfoViewWith:[responseObject objectForKey:@"msg"] ];

            }

        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            //失败回调
            [self hideHud];//根据情况来改变代码

        } ];

    }else
    {
       //写入需要的代码


    }


}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用uniapp的官方组件`uni-popup`来实现弹窗,同时使用`uni-form`组件来实现表单。上传图片可以使用`uni.chooseImage`方法来选择图片上传服务器上传数据可以使用`uni.request`方法发送数据服务器。 以下是一个简单的示例代码: ``` <template> <view> <button @click="showPopup">打开弹窗</button> <uni-popup v-model="show" position="bottom"> <uni-form> <uni-form-item label="标题"> <uni-input v-model="title"></uni-input> </uni-form-item> <uni-form-item label="内容"> <uni-input v-model="content"></uni-input> </uni-form-item> <uni-form-item label="上传图片"> <button @click="chooseImage">选择图片</button> </uni-form-item> <uni-form-item> <button @click="submit">提交</button> </uni-form-item> </uni-form> </uni-popup> </view> </template> <script> export default { data() { return { show: false, title: '', content: '', images: [] } }, methods: { showPopup() { this.show = true }, chooseImage() { uni.chooseImage({ count: 1, success: (res) => { this.images = res.tempFilePaths } }) }, submit() { uni.request({ url: 'your_url', method: 'POST', data: { title: this.title, content: this.content, images: this.images }, success: (res) => { console.log(res) } }) } } } </script> ``` 在这个示例中,`uni-popup`组件作为一个弹窗容器,包含一个`uni-form`表单,表单中包含了标题、内容、上传图片和提交按钮。当点击选择图片按钮时,调用`uni.chooseImage`方法选择图片并将选择的图片路径存储在`images`数组中。当点击提交按钮时,调用`uni.request`方法将表单数据图片上传服务器。你需要将`your_url`替换成你的服务器地址。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值