2017-09-08 96 views
0
- (void)requestAFHttpPost { 
UIImage *postData = [UIImage imageNamed:@"lunch2.png"]; 
NSData *data = UIImageJPEGRepresentation(postData, 0.5); 
NSString *postUrl=[@"https://ssl.ufxin.com/baseapi/appupload.php?key=youfangxin&version=2.3.10&platform=ios&userid=1332&action=house_pic&client=user&from=1" stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]; 
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager]; 

NSString *msgLength = [NSString stringWithFormat:@"%lu", data.length]; 

manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"multipart/form-data", @"text/html", @"image/jpeg", @"application/octet-stream", @"text/xml", nil]; 
[manager.requestSerializer setValue:msgLength forHTTPHeaderField:@"Content-Length"]; 

[manager POST:postUrl parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> _Nonnull formData) { 
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; 
    // set time formate 
    [formatter setDateFormat:@"yyyyMMddHHmmss"]; 
    NSString *dateString = [formatter stringFromDate:[NSDate date]]; 
    NSString *fileName = [NSString stringWithFormat:@"%@.jpeg", dateString]; 
    [formData appendPartWithFileData:data name:@"file" fileName:fileName mimeType:@"image/*"]; 
} progress:^(NSProgress * _Nonnull uploadProgress) { 

} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { 
    UFLog(@"success"); 
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { 
    UFLog(@"failure"); 
}];} 

使用AFNetworking3.0上傳圖像總是失敗(沒有錯誤),但我可以上傳圖片成功,同時使用郵遞員AFNetworking3.0上傳圖片總是失敗(不出錯)

+0

請檢查我的答案在這篇文章https://stackoverflow.com/questions/41233839/afnetworking-3-0-multipart-form-data-for-uploading-image-error/41241165#41241165 –

+0

這個失敗太 –

+0

任何人都幫助我?該方法給我所有的回調失敗 –

回答

0

對於目標C

- (void)requestAFHttpPost { 


    NSString *postUrl= @"https://ssl.ufxin.com/baseapi/appupload.php"; //add only url. 

    //set dictionay with key and value like this. 
    NSMutableDictionary *dic = [[NSMutableDictionary alloc] init]; 
    [dic setObject:@"youfangxin" forKey:@"key"]; 
    [dic setObject:@"=2.3.10" forKey:@"version"]; 
    [dic setObject:@"ios" forKey:@"platform"]; 
    [dic setObject:@"1332" forKey:@"userid"]; 
    [dic setObject:@"house_pic" forKey:@"action"]; 
    [dic setObject:@"user" forKey:@"client"]; 
    [dic setObject:@"1" forKey:@"from"]; 

    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager]; 
    manager.requestSerializer = [AFJSONRequestSerializer serializer]; 
    manager.responseSerializer = [AFJSONResponseSerializer serializer]; 

    [manager POST:postUrl parameters:dic constructingBodyWithBlock:^(id<AFMultipartFormData> _Nonnull formData) { 

     NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; 
     // set time formate 
     [formatter setDateFormat:@"yyyyMMddHHmmss"]; 
     NSString *dateString = [formatter stringFromDate:[NSDate date]]; 
     NSString *fileName = [NSString stringWithFormat:@"%@.png", dateString]; 

     NSData *data = UIImageJPEGRepresentation([UIImage imageNamed:@"lunch2.png"], 0.25); 

     [formData appendPartWithFileData:data name:@"file" fileName:fileName mimeType:@"image/png"]; 

    } progress:^(NSProgress * _Nonnull uploadProgress) { 

    } success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { 
     NSLog(@"%@", responseObject); 
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { 
     NSLog(@"%@", error.localizedDescription); 
    }]; 

} 

對於Swift。

 let manager = AFHTTPSessionManager() 
     manager.requestSerializer = AFJSONRequestSerializer() 
     manager.responseSerializer = AFJSONResponseSerializer() 

     let MainUrl = "your url" //add only url 

     //add key and value create dicionary 
     let sendDic = ["key" : "value", 
         "key"  : "value", 
         ] 

     manager.post(MainUrl, parameters: sendDic, constructingBodyWith: { (formdata) in 

      let image = UIImageJPEGRepresentation(UIImage.init(named: "lunch2.png")!, 0.25) 
      formdata.appendPart(withFileData: image!, name: "your image key", fileName: "fileName" + ".png" , mimeType: "image/png") 

     }, progress: { (progress) in 

     }, success: { (task, responseObject) in 

      print(responseObject!) 

     }, failure: { (task, error) in 

      print(error.localizedDescription) 
     }) 

Swift Code適合我。

+0

哦,我嘗試你的代碼,oc和swift全部失敗。你能指出我的問題嗎? –

+0

@Serx.lee它爲我工作。你有什麼問題呢 ?請給我解釋一下。 –

+0

發佈成功,但responseObject內容爲code = 0; content =(); msg =「圖片上傳失敗」,我用chrome可以上傳成功 –