图像未上传到服务器

时间:2013-05-13 10:46:50

标签: iphone ios objective-c image-uploading

我想将图像从设备上传到服务器。但它没有上传!即使我的返回成功值显示为null。成功值应为1或0.我的代码在此处给出。请告诉我,我的代码是否有任何错误。在此先感谢您的帮助。

-(void)ImageUpload{

NSString *urlString = [NSString stringWithFormat:@"%@upload.php", APIheader];

NSString *postLength = [NSString stringWithFormat:@"%d", [imgDATA length]];

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setHTTPMethod:@"POST"];
[request setURL:[NSURL URLWithString:urlString]];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody:imgDATA];

NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
if (connection) {

    NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];

    NSLog(@"return string: %@",returnString);

    returnString = [returnString stringByReplacingOccurrencesOfString:@"(" withString:@"["];
    returnString = [returnString stringByReplacingOccurrencesOfString:@")" withString:@"]"];
    returnString = [returnString stringByReplacingOccurrencesOfString:@";" withString:@""];
    SBJsonParser *parser = [[SBJsonParser alloc]init];
    NSArray *array = (NSArray *)[parser objectWithString:returnString error:nil];
    NSString *status = [NSString stringWithFormat:@"%@",[[array objectAtIndex:0]objectForKey:@"success"]];

    if ([status isEqualToString:@"1"]) {

        NSLog(@"Image Updated");
    }
    else{

        NSLog(@"status is: %@",status);
    }
}

[request release];

}

4 个答案:

答案 0 :(得分:2)

您可以使用CoreGraphics'方法UIImagePNGRepresentation(UIImage *image),该方法返回NSData并保存。如果您想再次将其转换为UIImage,请使用[UIimage imageWithData:(NSData *data)]方法创建它。一些与此问题相关的问题post image to server in iphoneHow to convert image into binary format in iOS? 3次给出相同的答案。

- (void)sendImageToServer {
       UIImage *yourImage= [UIImage imageNamed:@"image.png"];
       NSData *imageData = UIImagePNGRepresentation(yourImage);
       NSString *postLength = [NSString stringWithFormat:@"%d", [imageData length]];

       // Init the URLRequest
       NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
       [request setHTTPMethod:@"POST"];
       [request setURL:[NSURL URLWithString:[NSString stringWithString:@"http://yoururl.domain"]]];
       [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
       [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
       [request setHTTPBody:imageData];

       NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
       if (connection) {
          // response data of the request
       }
       [request release];
 }

答案 1 :(得分:0)

使用此代码将任何图像上传到服务器。这适合我。 :)

   UIImage *yourImage= [UIImage imageNamed:@"image.png"];
   NSData *imageData = UIImagePNGRepresentation(yourImage);
   NSString *postLength = [NSString stringWithFormat:@"%d", [imageData length]];

   // Init the URLRequest
   NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
   [request setHTTPMethod:@"POST"];
   [request setURL:[NSURL URLWithString:[NSString stringWithString:@"http://yoururl.domain"]]];
   [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
   [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
   [request setHTTPBody:imageData];

   NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
   if (connection) {
      // response data of the request
   }

答案 2 :(得分:0)

试试这个

// create request
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];                                    
[request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
[request setHTTPShouldHandleCookies:NO];
[request setTimeoutInterval:30];
[request setHTTPMethod:@"POST"];

// set Content-Type in HTTP header
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
[request setValue:contentType forHTTPHeaderField: @"Content-Type"];

// post body
NSMutableData *body = [NSMutableData data];

// add params (all params are strings)
for (NSString *param in _params) {
    [body appendData:[[NSString stringWithFormat:@"--%@\r\n", BoundaryConstant] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n", param] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithFormat:@"%@\r\n", [_params objectForKey:param]] dataUsingEncoding:NSUTF8StringEncoding]];
}

// add image data
NSData *imageData = UIImageJPEGRepresentation(imageToPost, 1.0);
if (imageData) {
    [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"image.jpg\"\r\n", FileParamConstant] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithString:@"Content-Type: image/jpeg\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:imageData];
    [body appendData:[[NSString stringWithFormat:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
}

[body appendData:[[NSString stringWithFormat:@"--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];

// setting the body of the post to the reqeust
[request setHTTPBody:body];

// set the content-length
NSString *postLength = [NSString stringWithFormat:@"%d", [body length]];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];

// set URL
[request setURL:requestURL];

请注意

“BoundaryConstant”基本上是变量“boundary”,它本质上是一个随机的(NSString *),“FileParamConstant”基本上是你的“filename.jpg”

答案 3 :(得分:0)

你也可以通过导入ASIFormDataRequest calss,Here是告诉为什么要使用它的链接,在下载类之后就这样做了,

uploading image to server using ASIFormDataReqest

希望它会帮助你......