【发布时间】:2010-03-31 11:36:42
【问题描述】:
我正在使用目标 C 中的 multipart/form-data 向服务器发送一些数据和图像。 请给我一些 PHP 代码,我如何将图像保存在服务器上,我能够在服务器上获取与图像一起传递的其他变量。请查看我的 obj C 代码和 php 并告诉我哪里错了。
我们将非常感谢您的帮助。
我在这里发出 POST 请求。
////////////////////
NSString *stringBoundary, *contentType, *baseURLString, *urlString;
NSData *imageData;
NSURL *url;
NSMutableURLRequest *urlRequest;
NSMutableData *postBody;
// Create POST request from message, imageData, username and password
baseURLString = @"http://localhost:8888/Test.php";
urlString = [NSString stringWithFormat:@"%@", baseURLString];
url = [NSURL URLWithString:urlString];
urlRequest = [[[NSMutableURLRequest alloc] initWithURL:url] autorelease];
[urlRequest setHTTPMethod:@"POST"];
// Set the params
NSString *path = [[NSBundle mainBundle] pathForResource:@"LibraryIcon" ofType:@"png"];
imageData = [[NSData alloc] initWithContentsOfFile:path];
// Setup POST body
stringBoundary = [NSString stringWithString:@"0xKhTmLbOuNdArY"];
contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", stringBoundary];
[urlRequest addValue:contentType forHTTPHeaderField:@"Content-Type"];
// Setting up the POST request's multipart/form-data body
postBody = [NSMutableData data];
[postBody appendData:[[NSString stringWithFormat:@"\r\n\r\n--%@\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"source\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithString:@"lighttable"] dataUsingEncoding:NSUTF8StringEncoding]]; // So Light Table show up as source in Twitter post
[postBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"title\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithString:book.title] dataUsingEncoding:NSUTF8StringEncoding]]; // title
[postBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"isbn\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithString:book.isbn] dataUsingEncoding:NSUTF8StringEncoding]]; // isbn
[postBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"price\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithString:txtPrice.text] dataUsingEncoding:NSUTF8StringEncoding]]; // Price
[postBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"condition\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithString:txtCondition.text] dataUsingEncoding:NSUTF8StringEncoding]]; // Price
NSString *imageFileName = [NSString stringWithFormat:@"photo.jpeg"];
[postBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"upload\"; filename=\"%@\"\r\n",imageFileName] dataUsingEncoding:NSUTF8StringEncoding]];
//[postBody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"upload\"\r\n\n\n"]dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[@"Content-Type: image/jpeg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:imageData];
[postBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
// [postBody appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
NSLog(@"postBody=%@", [[NSString alloc] initWithData:postBody encoding:NSASCIIStringEncoding]);
[urlRequest setHTTPBody:postBody];
NSLog(@"Image data=%@",[[NSString alloc] initWithData:imageData encoding:NSASCIIStringEncoding]);
// Spawn a new thread so the UI isn't blocked while we're uploading the image
[NSThread detachNewThreadSelector:@selector(uploadingDataWithURLRequest:) toTarget:self withObject:urlRequest];
我的方法 uploadingDataWithURLRequest 我将请求发布到服务器...
这是我的php代码
?php
$title = $_POST['title'];
$isbn = $_POST['isbn'];
$price = $_POST['price'];
$condition = $_POST['condition'];
$image=$_FILES['image']['name'];
if($image)
{
$filename = 'newimage.jpeg';
file_put_contents($filename, $image);
echo "image is there";
}
else
{
echo "image is nil";
}
?>
我无法在服务器上获取图像,请帮助我在哪里出错。
【问题讨论】:
-
我有一个代码,我只将图像发送到服务器。在服务器上必须有“上传”目录,我的代码会将图像发送到 php 文件,然后 php 将该文件上传到“上传”目录......我不知道如何在此处添加文件或者如果有任何方法....管理员建议我解决方案或在 bharat.jagtap@bitcode.in 上给我发一封测试邮件,我会将这些文件发送给您...
标签: php iphone objective-c