Upload file not working vie HTTP Post

2019-06-09 21:21发布

This function is supposed to upload an UIImage to an web form per HTTP Post. I used extracts from some posts here and on othe rsites and got to this in the end.

But it doesn't work, it workes like an GET Method for url, at least for me. Could you pelase test it, too, or help me, where the mistake could be?

I DON'T want to use a framework like ASIHTTPRequest please.

- (NSString *)postImage:(UIImage *)image toURL:(NSURL *)url {
NSData *imageData = UIImagePNGRepresentation(image);
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"POST"];
NSString *boundary = [NSString stringWithFormat:@"---------------------------%i", (int)[[NSDate date] timeIntervalSinceReferenceDate]*100];
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];

NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"Photo\"; filename=\"ipodfile.png\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"Content-Type: image/png\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:imageData]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:body];
[request addValue:[NSString stringWithFormat:@"%d", body.length] forHTTPHeaderField: @"Content-Length"];

NSURLResponse *response = NULL;
NSError *requestError = NULL;
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&requestError];
return [[[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding] autorelease];    
}

Thanks, Vincento

3条回答
等我变得足够好
2楼-- · 2019-06-09 22:06

Are you sure that the image you are sending is not null? If you are trying to send an empty POST data it will be changed back to GET.

查看更多
Deceive 欺骗
3楼-- · 2019-06-09 22:11
   - (void) postCommentsWithImage : message : (NSString*)message imageData : (NSData*)data
           {
       NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];

    [theRequest setHTTPMethod:@"POST"];


    [theRequest setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];


    if(data)
    {

        NSString* pictureDataString = [NSData encode:data];
        [dataDictionary setObject:pictureDataString forKey:@"imagebyte"];
    }
    else 
            {
        [dataDictionary setObject:@"" forKey:@"imagebyte"];
    }

    NSString *theBodyString = [dataDictionary JSONRepresentation];

    [dataDictionary release];

    NSData *theBodyData = [theBodyString dataUsingEncoding:NSUTF8StringEncoding];   
    [theRequest setHTTPBody:theBodyData];  

    [theRequest setHTTPBody:theBodyData];  

    NSLog(@"theRequest=%@", theRequest);
    self.connection = [[NSURLConnection alloc] initWithRequest:theRequest    delegate:self];

    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;

}

查看更多
该账号已被封号
4楼-- · 2019-06-09 22:13

Seems all are ok. (Is all are ok on server side) ? I had put working example on http://code.developwithus.com/iphone/upload-image-and-data-with-iphone-sdk/ , will you try that?

查看更多
登录 后发表回答