我们一直在为iOS和Android开发应用程序。我们的一个视图使用POST将图片上传到服务器。 Android版本在我们的本地服务器和网站中都可以使用。但是,iOS版本目前只在我们的本地服务器上运行。以下是用于编写POST请求的代码:
NSData * imageData = UIImageJPEGRepresentation(image,1.0);
// Create the request.
NSString* afterURL = [NSString stringWithFormat:@"upload.php?username=%@",username];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc]
initWithURL:[NSURL URLWithString:
[NSString stringWithFormat:@"%@%@",url,afterURL]]];
[request setHTTPMethod:@"POST"];
//------------------------------------------------------
// used code from this tutorial: http://www.iriphon.com/2011/11/09/ios-uploading-an-image-from-your-iphone-to-a-server/
NSString *boundary = @"-----InstantLabor";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];
NSMutableData *body = [NSMutableData data];
// Now we need to append the different data 'segments'. We first start by adding the boundary.
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
// This line of code came from http://stackoverflow.com/questions/20728317/upload-image-to-the-php-server-from-ios
// By creating the string this way instead of the other way, we were able to successfully upload the picture!
/*
[body appendData:[[NSString stringWithString:
[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"userfile\"; filename=\"%@.jpg\"\r\n", @"dl"]]
dataUsingEncoding:NSUTF8StringEncoding]];
*/
[body appendData:[@"Content-Disposition: form-data; name=\"attachment[userfile]\";filename=\"dl.jpg\"\r\n"
dataUsingEncoding:NSUTF8StringEncoding]];
// We now need to tell the receiver what content type we have
// In my case it's a png image. If you have a jpg, set it to 'image/jpg'
[body appendData:[@"Content-Type: image/jpg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
// Now we append the actual image data
[body appendData:[NSData dataWithData:imageData]];
// and again the delimiting boundary
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
// adding the body we've created to the request
[request setHTTPBody:body];
//-----------------------------------------------------
// Create url connection and fire request
_parsedData = nil;
[NSURLConnection connectionWithRequest:request delegate:self];
当我们试图让它在本地服务器上工作时,上面的代码在我们切换之前不起作用
[body appendData:[@"Content-Disposition: form-data; name=\"attachment[userfile]\";filename=\"dl.jpg\"\r\n"
dataUsingEncoding:NSUTF8StringEncoding]];
带
[body appendData:[[NSString stringWithString:
[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"userfile\"; filename=\"%@.jpg\"\r\n", @"dl"]]
dataUsingEncoding:NSUTF8StringEncoding]];
即使创建相同的字符串,一个工作,另一个不工作;但是在连接到网站时它们都不起作用。我们尝试的所有东西最终都吐出来了
Undefined index: userfile
回到申请表。由于其中一个有效,我们知道这不是内存问题或特权问题。我们不知道还有什么可能导致这个问题。
提前感谢您的帮助。
答案 0 :(得分:0)
经过两天的调整,测试。等我们没有运气解决问题。相反,我们决定合并AFNetworking并按照Upload image from iOS to PHP进行操作,以使我们的图片上传工作。