1

我正在尝试将图像上传到 json 服务器,但 url 请求为空。下面是我正在使用的代码。

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{

UIImage *imageToScale=[info objectForKey:UIImagePickerControllerOriginalImage];
UIImage *img = imageToScale;
//NSLog(@"finalImage---------------------------------%@",self.finalImage);
NSData *imageData = UIImageJPEGRepresentation(img, 0.40);

profileImg = profilePic;

NSArray *arrImg = [profileImg componentsSeparatedByString:@"/"];
NSString *strImg1 = [arrImg objectAtIndex:[arrImg count]-2];
NSString *strImg2 = [arrImg lastObject];
NSLog(@"img2-----------------%@",strImg2);
NSString *pImg = [NSString stringWithFormat:@"%@%@",strImg1, strImg2];
NSLog(@"img-----------------%@",pImg);
NSString *urlString = [NSString stringWithFormat:@"http://www.funnyghg.co/overheadPost.php?userId=%@&type=Photo&text=Demo test&image=%@&location=Chandigarh&Upload=Upload", uid, pImg];
NSLog(@"url :%@", urlString);
// setting up the request object now
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];

NSString *boundary = @"---------------------------14737809831466499882746641449";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];

/*
 now lets create the body of the post
 */
NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"userfile\";filename=\"1.jpg\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:imageData]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
// setting the body of the post to the reqeust
[request setHTTPBody:body];

// now lets make the connection to the web
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];

NSLog(@"returnString------------%@",returnString);

[self dismissModalViewControllerAnimated:YES];
[appDelegate showAllNavItems];
[appDelegate navFrame];

if([returnString isEqualToString:@"true"])
{

    if([profile isEqualToString:@"friend"])
    {
        [self callServerFriend];
    }
    if([profile isEqualToString:@"public"])
    {
        [self callServerPublic];
    }
}
}

请指导以上。提前致谢。

4

1 回答 1

0

这是因为在您的代码中,在 Demo 和 test 之间的 urlString 中有一个空格。请删除它。

前 :

NSString *urlString = [NSString stringWithFormat:@"http://www.funnyghg.co/overheadPost.php?userId=%@&type=Photo&text=Demo test&image=%@&location=Chandigarh&Upload=Upload", uid, pImg];

NSLog(@"url :%@", urlString);

后 :

NSString *urlString = [NSString stringWithFormat:@"http://www.funnyghg.co/overheadPost.php?userId=%@&type=Photo&text=Demotest&image=%@&location=Chandigarh&Upload=Upload", uid, pImg];

NSLog(@"url :%@", urlString);

我希望它现在可以工作。

于 2013-04-12T08:04:02.947 回答