1
votes

I use AFNetworking2.0 upload image, code like:


    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager.requestSerializer setValue:PARSE_APPLICATION_ID forHTTPHeaderField:@"X-Parse-Application-Id"];
    [manager.requestSerializer setValue:PARSE_REST_API_KEY forHTTPHeaderField:@"X-Parse-REST-API-Key"];
    [manager.requestSerializer setValue:@"image/jpeg" forHTTPHeaderField:@"Content-Type"];

    UIImage *image = [UIImage imageNamed:@"resource.bundle/1.jpg"];
    NSData *imageData = UIImageJPEGRepresentation(image, 0.5);

    [manager POST:@"https://api.parse.com/1/files/kingiol.jpg" parameters:nil constructingBodyWithBlock:^(id formData) {
        [formData appendPartWithFileData:imageData name:@"avatar" fileName:@"avatar.jpg" mimeType:@"image/jpeg"];
    } success:^(NSURLSessionDataTask *task, id responseObject) {
        NSLog(@"success:%@", responseObject);
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"Error:%@", error);
    }];

run this and log success block, and i get the upload response:


    success:{
    name = "84cff2e0-605c-44de-909f-655379884b67-kingiol.jpg";
    url = "http://files.parse.com/5a56e6bf-a4b5-4155-9bf3-238550da126b/84cff2e0-605c-44de-909f-655379884b67-kingiol.jpg";
    }

but, when i copy the url, then open with chrome, the image is a invalidate image.

Please help

1
Have you checked the image using the Parse data browser?David Schwartz
I pick up the response url, then open with chrome browser, then download the file, then I open the downloaded file, an alert message show this file isn't a validate imageKingiol

1 Answers

1
votes

It seems that your file contains the whole request that you've made. Try to open that file with a text editor like TextEdit, i've done it and i've found something like this:

--Boundary+0xAbCdEfGbOuNdArY
Content-Disposition: form-data; name="avatar"; filename="avatar.jpg"
Content-Type: image/jpeg

<All binary image data>
....
..
..

--Boundary+0xAbCdEfGbOuNdArY--

Why don't you use the Parse SDK functions? Look this:

NSData *imageData = UIImagePNGRepresentation(image);
PFFile *imageFile = [PFFile fileWithName:@"image.png" data:imageData];

PFObject *userPhoto = [PFObject objectWithClassName:@"UserPhoto"];
userPhoto[@"imageName"] = @"My trip to Hawaii!";
userPhoto[@"imageFile"] = imageFile;
[userPhoto saveInBackground];

see https://parse.com/docs/ios_guide#files-images/iOS

hope it helps