Uploading image to server in iphone application

前端 未结 2 1813
感情败类
感情败类 2021-01-07 10:08

I want upload image to sever in iphone app I am using following code for PHP

but using this code i get error displayed on page when i run localhost/upload.php;

相关标签:
2条回答
  • 2021-01-07 10:34

    if you think error in php code then first try this print_r($_FILES); and see what did you get in the $_FILES['userfile']

    0 讨论(0)
  • 2021-01-07 10:36

    try this and also did you check in php by print_r($_FILES);

                        NSString *urlString = @"http://localhost/upload.php";
    
                        NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlString] cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:60];
                        [request setHTTPMethod:@"POST"];
    
                        NSString *boundary = [NSString stringWithString:@"---------------------------14737809831466499882746641449"];
                        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=\"userfile\"; filename=\"%@\"\r\n", filename] dataUsingEncoding:NSUTF8StringEncoding]];
                        [body appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
                        [body appendData:imageData];
                        [body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
                        [request setHTTPBody:body];
    
                        NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
                        NSString *returnString = [[[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding] autorelease];
    
    0 讨论(0)
提交回复
热议问题