【发布时间】:2014-03-31 13:03:17
【问题描述】:
我正在努力学习 AFNetworking,我从他们的 Github 中获取了样本。我不知道使用php 在dataToPost 中设置哪些参数。我是Objective-C 和php 的新手。有人可以看看我的 sn-p 和我的php 看看我缺少什么。很难找到 AFNetworking 的“上传”教程,但那里有大量的 JSON 教程。
我想使用NSMutableURLRequest,因为最终我想从数组或UITableview 上传;到目前为止,这里是 mt 代码:
PHP:
<?php
header("Content-Type: application/json");
$uploaddir = './'; //Uploading to same directory as PHP file
$file = basename($_FILES['userfile']['name']);
$uploadFile = $file;
$randomNumber = rand(0, 99999);
$newName = $uploadDir . $randomNumber . $uploadFile;
if (!is_uploaded_file($_FILES['userfile']['tmp_name'])) {
$result = array("success" => false);
echo json_encode($result);
exit();
}
if ($_FILES['userfile']['size']> 300000) {
$result = array("success" => false, "message" =>"the uploaded file is too big");
echo json_encode($result);
exit();
}
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $newName)) {
$postsize = ini_get('post_max_size'); //Not necessary, I was using these
$canupload = ini_get('file_uploads'); //server variables to see what was
$tempdir = ini_get('upload_tmp_dir'); //going wrong.
$maxsize = ini_get('upload_max_filesize');
//echo "http://localhost:8888/upload/{$file}" . "\r\n" . $_FILES['userfile']['size'] . "\r\n" . $_FILES['userfile']['type'] ;
$result = array("success" => true,
"code" => 0,
"message" => "success",
"postsize" => $postsize,
"canupload" => $canupload,
"tempdir" => $tempdir,
"maxsize" => $maxsize);
echo json_encode($result);
}
?>
Xcode:
// 1. Create AFHTTPRequestSerializer which will create your request.
AFHTTPRequestSerializer *serializer = [AFHTTPRequestSerializer serializer];
// 2. Create an NSMutableURLRequest.
NSMutableURLRequest *request =
[serializer multipartFormRequestWithMethod:@"POST" URLString:@"http://my.com/upload/upload.php"
parameters:nil
constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
[formData appendPartWithFileData:imageData
name:@"userfile"
fileName:@"myimage.jpg"
mimeType:@"image/jpeg"];
}];
// 3. Create and use AFHTTPRequestOperationManager to create an AFHTTPRequestOperation from the NSMutableURLRequest that we just created.
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
AFHTTPRequestOperation *operation =
[manager HTTPRequestOperationWithRequest:request
success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"Success %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Failure %@", error.description);
}];
// 4. Set the progress block of the operation.
[operation setUploadProgressBlock:^(NSUInteger __unused bytesWritten,
NSInteger totalBytesWritten,
NSInteger totalBytesExpectedToWrite) {
NSLog(@"Wrote %ld/%ld", (long)totalBytesWritten, (long)totalBytesExpectedToWrite);
}];
// 5. Begin!
[operation start];
错误(已解决):
2014-03-31 15:37:46.921 TestingUpload[7190:60b] Wrote 32768/59063
2014-03-31 15:37:46.922 TestingUpload[7190:60b] Wrote 59063/59063
2014-03-31 15:37:46.923 TestingUpload[7190:60b] Wrote 32768/59063
2014-03-31 15:37:46.923 TestingUpload[7190:60b] Wrote 59063/59063
2014-03-31 15:37:46.925 TestingUpload[7190:60b] Success {
canupload = 1;
code = 0;
maxsize = 32M;
message = success;
postsize = 32M;
success = 1;
tempdir = "/Applications/MAMP/tmp/php";
}
2014-03-31 15:37:46.927 TestingUpload[7190:60b] Success {
canupload = 1;
code = 0;
maxsize = 32M;
message = success;
postsize = 32M;
success = 1;
tempdir = "/Applications/MAMP/tmp/php";
}
感谢您的帮助和解释。谢谢!
【问题讨论】:
-
我没有测试过,但是随便看了一下,PHP看起来还不错。
-
检查上面。我不再关心太大了,因为它总是与设备大小相同。
-
如果您想小心一些恶意用户注意到文件上传的 URL 并决定将 2 GB 的文件上传到您的服务器上,那么太大的检查很重要。这就是为什么它通常在那里;以尽量减少被一些不法之徒滥用的机会。在您的测试中没什么大不了的,但在您投入生产之前,请考虑重新添加该代码(但使用 JSON 响应)。
-
是的,效果很好...我添加了一个 for 循环,看看我是否可以上传 2 张以上的图片,效果很好。你在说什么文件大小?
-
@Rob 将 PHP 更新为我对 JSON/size 错误的解释。看起来怎么样?
标签: php objective-c afnetworking-2