Iphone 上传文件不工作vie HTTP Post

Iphone 上传文件不工作vie HTTP Post,iphone,http,post,sdk,upload,Iphone,Http,Post,Sdk,Upload,此函数用于根据HTTP Post将UIImage上载到web表单。 我在这里和其他网站上使用了一些帖子的摘录,最终达到了这个目的 但它不起作用,它就像url的GET方法,至少对我来说是这样。 你能不能也测试一下,或者帮助我,错误在哪里 我不想使用像ASIHTTPRequest这样的框架 - (NSString *)postImage:(UIImage *)image toURL:(NSURL *)url { NSData *imageData = UIImagePNGRepresentation

此函数用于根据HTTP Post将UIImage上载到web表单。 我在这里和其他网站上使用了一些帖子的摘录,最终达到了这个目的

但它不起作用,它就像url的GET方法,至少对我来说是这样。 你能不能也测试一下,或者帮助我,错误在哪里

我不想使用像ASIHTTPRequest这样的框架

- (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];    
}
谢谢, 文森托似乎一切都好。 (服务器端是否都正常)?
我已经把工作示例放在上面了,你能试试吗?

你确定你发送的图像不是空的吗?如果您试图发送一个空的POST数据,它将被更改回GET

   - (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;

}

如果([UIImage imagename:@“imagename.png”]!=NULL){NSLog(@“notnull”);}记录的不是NULL,那么我猜,图像不是NULL;)我试过你的代码,它似乎对我有用。NC显示:POST/HTTP/1.1主机:本地主机:8080用户代理:test_for_stackoverflow/1.0 CFNetwork/485.13.8 Darwin/10.7.0内容长度:2796内容类型:多部分/表单数据;boundary=------------------------------------1083820068您的方法也不起作用,我必须删除'NSString*postData=[Webservices getHTTPBodyParamsFromDictionary:record]//这里Webservices是我的类名[postbody appendData:[postData dataUsingEncoding:NSUTF8StringEncoding];'第二部分,因为我没有那个方法,我也不知道该传递什么。所以您认为这是服务器端错误?