无法在iOS服务器上传图像?

时间:2014-02-04 06:57:32

标签: ios iphone objective-c xcode web-services

我正在尝试将图像上传到服务器,但我无法将图像上传到服务器。 我从iPad库中选择图像并将其发送到服务器。

这是我的代码:

-(void)ViewDidLoad{

    SelectPickerimageView = [[UIImageView alloc]initWithFrame:CGRectMake(10, 600, 200,     200)];
    SelectPickerimageView.backgroundColor = [UIColor greenColor];
    [self.view addSubview:SelectPickerimageView];
}



-(void)method_OpenImagePickerLibrary{

    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;
    picker.allowsEditing = YES;
    picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

    [self presentViewController:picker animated:YES completion:NULL];
}



-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    UIImage *image=[info objectForKey:UIImagePickerControllerEditedImage];
    SelectPickerimageView.image=image;
    [self dismissModalViewControllerAnimated:YES];
}


-(void)pushUpload{

    NSData *imgaeData =  UIImageJPEGRepresentation(SelectPickerimageView.image, 90);
    NSString *urlString = @"http://projectsatseoxperts.net.au/fishing/api/image.php";

    NSMutableURLRequest *request = [[NSMutableURLRequest alloc]init];
    [request setURL:[NSURL URLWithString:urlString]];
    [request setHTTPMethod:@"POST"];

     NSLog(@"request is:%@",request);

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


     NSLog(@"request is:%@",request);


    NSMutableData *body = [NSMutableData data];
    //[body appendData:[[NSString stringWithFormat:@"\r\n--%\r\n",boundary]  dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[@"Content-Disposition: form-data; name=\"uploadedfile\"; filename=\".jpg\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[NSData dataWithData:imgaeData]];
    [body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [request setHTTPBody:body];


    NSLog(@"request is:%@",request);

    NSData *returnData = [NSURLConnection sendSynchronousRequest:request  returningResponse:nil error:nil];
    NSLog(@"returnData: %@", returnData);

    NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
    NSLog(@"returnString: %@", returnString);

 }


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

给我回复:

您无权访问请求的对象。 它受读保护或服务器无法读取。

  

returnString:禁止访问! * / - >

禁止访问!

您无权访问请求的对象。它受读保护或服务器无法读取。

如果您认为这是服务器错误,请与网站管理员联系。

  

错误403

     

projectsatseoxperts.net.au

     

Tue Feb 4 06:09:05 2014 Apache mod_fcgid / 2.3.7 mod_auth_pgsql / 2.0.3

当我运行代码时,NSLog(@"returnData: %@", returnData);有1066个字节。

现在我无法弄清楚这个问题是什么? 我所做的?我联系我的网站开发人员,他说服务器一切正常。 那么为什么我会收到这个错误。

还有一件事我如何查看我的帖子数据?

喜欢

/*
NSURL *url=[NSURL      URLWithString:@"http://projectsatseoxperts.net.au/fishing/api/postUpload.php"];


NSString *post =[[NSString alloc] initWithFormat:@"txt_user_Id=%@ &  txt_streetnumber=%@ & txt_streetname=%@ & txt_state=%@ & txt_country=%@ & txt_suburb=%@ &  txt_postalcode=%@ & txt_latitude=%@ & txt_longitude=%@ & txt_submitupload=%@",txt_user_Id.text,txt_streetnumber.text,txt_streetname.text,txt_state.text,txt_country.text,txt_suburb.text,txt_postalcode.text, txt_latitude.text , txt_longitude.text,txt_submitupload.text];

NSLog(@"Post is: %@",post);

NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSLog(@"postData is: %@",postData);

*/

在这里行动我无法检查我的帖子数据。

非常欢迎任何想法或建议。

1 个答案:

答案 0 :(得分:0)

可能的问题:

  1. 行中
    NSString *contentType = 
     [NSString stringWithFormat:@"multipart/form-data; boundary = %@",boundary];
    

    boundary之后和=之后不得有空格。

    修正:

    NSString *contentType = 
     [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
    

    您可以用双引号括起边界。

  2. 行中
    [body appendData:[@"Content-Disposition: form-data; name=\"uploadedfile\"; filename=\".jpg\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
    

    您错过了指定完整的文件名。

  3. 可能是拼写错误:

    -(void)ViewDidLoad