ios - AFNetworking上传图片,
高洛峰
高洛峰 2017-04-18 09:18:14
0
3
293

我这里用AFNetworking上传图片
报The data couldn’t be read because it isn’t in the correct format.有人帮忙看看是哪里出问题了么?下面是我写的代码。

AFHTTPRequestOperationManager *m = [AFHTTPRequestOperationManager manager]; m.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html", @"application/json", nil]; m.requestSerializer = [AFHTTPRequestSerializer serializer]; [m.requestSerializer setValue:@"multipart/form-data" forHTTPHeaderField:@"Content-Type"]; NS_APPDebugLog(@"请求链接:%@/%@", urlString, parametersDic); [m POST:@"http://59.48.96.118:7001/portal/SI_USR00025/upload.do" parameters:nil constructingBodyWithBlock:^(id _Nonnull formData) { for (int i = 0; i < imagePickerArray.count; i++) { UIImage *portraitImg = imagePickerArray[i]; portraitImg = [UIImage scaleToSize:portraitImg]; portraitImg = [portraitImg fixOrientation]; NSData *imageData = [NSData compressImage:portraitImg]; [formData appendPartWithFileData:imageData name:[NSString stringWithFormat:@"img.img%d", i+1] fileName:@"image.png" mimeType:@"image/png"]; } } success:^(AFHTTPRequestOperation * _Nonnull operation, id _Nonnull responseObject) { NSLog(@"成功"); } failure:^(AFHTTPRequestOperation * _Nullable operation, NSError * _Nonnull error) { NSLog(@"失败"); dispatch_async(dispatch_get_main_queue(),^{failure(error);}); }];
高洛峰
高洛峰

拥有18年软件开发和IT教学经验。曾任多家上市公司技术总监、架构师、项目经理、高级软件工程师等职务。 网络人气名人讲师,...

reply all (3)
PHPzhong

A few questions here:

  1. mime settings have some contradictions:

    [m.requestSerializer setValue:@"multipart/form-data" forHTTPHeaderField:@"Content-Type"];
    [formData appendPartWithFileData:imageData name:[NSString stringWithFormat:@"img.img%d", i+1] fileName:@"image.png" mimeType:@"image/png"];

    You have set two types of mime in these two places,multipart/form-dataimage/png. Which one is correct? Need to communicate with the backend.

  2. Contact the backend, have you received your request? What content is requested? What did he return? This error seems to be usually reported when parsing json. Maybe the background does not return valid json. In addition, your request is to upload multiple files at once. I don’t know if your backend interface supports it. You can try uploading only one picture.

  3. There is no need to adjust the AFNetworking blockdispatch_async(dispatch_get_main_queue()...It has already helped you return to the main thread for execution.

    大家讲道理

    Can I print out the data of imageData?

      洪涛

      Have you solved it?
      I also encountered the report The data couldn’t be read because it isn’t in the correct format.
      But in fact the image was uploaded successfully
      Client code: - (IBAction)upload:(id)sender {

      AFHTTPSessionManager *manager = [AFHTTPSessionManager manager]; NSString *url3 = @"http://127.0.0.1:5000/upload";

      [manager POST:url3 parameters:nil constructingBodyWithBlock:^(id _Nonnull formData) {

      UIImage *image = [UIImage imageNamed:@"star"]; NSData *imageData = UIImagePNGRepresentation(image); NSString *fileName = @"star.png"; [formData appendPartWithFileData:imageData name:@"file" fileName:fileName mimeType:@"image/png"]; } progress:^(NSProgress *_Nonnull uploadProgress) { } success:^(NSURLSessionDataTask *_Nonnull task, id _Nullable responseObject) { NSLog(@"upload completed:%@",responseObject); } failure:^(NSURLSessionDataTask *_Nullable task, NSError * _Nonnull error) { NSLog(@"upload failed"); NSLog(@"%@",error.localizedDescription); }];

      }

      Backend code (python):
      class receive(restful.Resource):

      def post(self): file = request.files['file'] file.save(os.path.join(app.config['UPLOAD_FOLDER'], 'star2.png')) return 'upload completed'

      api.add_resource(receive, '/upload', methods=['POST'])

        Latest Downloads
        More>
        Web Effects
        Website Source Code
        Website Materials
        Front End Template
        About us Disclaimer Sitemap
        php.cn:Public welfare online PHP training,Help PHP learners grow quickly!