0

我想将图像从设备上传到服务器。但它没有上传!甚至我的返回成功值也显示为空。成功值应该是 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

4 回答 4

2

您可以使用CoreGraphics' 方法UIImagePNGRepresentation(UIImage *image),该方法返回NSData并保存它。如果您想将其转换为再次使用方法UIImage创建它。[UIimage imageWithData:(NSData *data)]与此帖子图像相关的一些问题到iphone 中的服务器以及 如何在 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];
 }
于 2013-05-13T10:51:10.950 回答
0

您也可以通过导入 ASIFormDataRequest 类来做到这一点,是告诉为什么要使用这个的链接,下载类后就这样做,

使用 ASIFormDataReqest 将图像上传到服务器

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

于 2013-05-13T12:24:35.380 回答
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
   }
于 2013-05-13T11:09:47.903 回答
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”

于 2013-05-13T11:49:38.843 回答