Upload image to server with iOS not working

2019-04-13 07:23发布

I am trying to upload an image to my php server but it's not working. When I try to upload the file, the screen just freezes (this is expected - I'm using sendSynchronousRequest until I can get this to work) for about a minute and then it returns an empty string as a response. I've been working on this for over a day and can't seem to get it to work. Furthermore, the image never actually uploads to the server (I checked). Thanks in advance!

// Uploading the image
-(IBAction)uploadImage:(id)sender{
[self uploadImage:UIImageJPEGRepresentation(productImageView.image, 90)    filename:@"image.jpg"];
}

- (BOOL)uploadImage:(NSData *)imageData filename:(NSString *)filename{

// This isn't actually my url btw
NSString *urlString = @"http://www.mysite.com/uploadImage.php";


// setting up the request object now
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];

/*
 add some header info now
 we always need a boundary when we post a file
 also we need to set the content type

 You might want to generate a random boundary.. this is just the same
 as my output from wireshark on a valid html 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:@"rn--%@rn",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Disposition: form-data; name=\"userfile\"; filename=\"ipodfile.jpg\"rn" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Type: application/octet-streamrnrn" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:imageData]];
[body appendData:[[NSString stringWithFormat:@"rn--%@--rn",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];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Responce"
                                                message:returnString
                                               delegate:nil
                                      cancelButtonTitle:@"OK"
                                      otherButtonTitles:nil];
[alert show];
return ([returnString isEqualToString:@"OK"]);
}

And my php file:

<?php
$uploaddir = ''; // Put in same directory as PHP file for debugging purposes
$file = basename($_FILES['userfile']['name']);
$uploadfile = $uploaddir . $file;

if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
    echo "Your file is called {$file}";
}

?>

2条回答
Melony?
2楼-- · 2019-04-13 08:03
狗以群分
3楼-- · 2019-04-13 08:18

You missed the escaping of the CRLF characters, e.g.

Change from

[body appendData:[[NSString stringWithFormat:@"rn--%@rn",boundary] 

to

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

The complete fix:

/*
 now lets create the body of the post
 */
NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]]; // note: the preceding "\r\n" may be problematic if the server does not properly implement the "preamble" rule as specified in RFC 2046
[body appendData:[@"Content-Disposition: form-data; name=\"userfile\"; filename=\"ipodfile.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]];
查看更多
登录 后发表回答