AFNetworking3.0上传图片总是失败(不是错误)

时间:2017-09-08 08:45:07

标签: ios afnetworking

- (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上传图片总是失败(不是错误),但我可以在使用邮递员时上传此图片

1 个答案:

答案 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为我工作。