我必须将图像从 iphone 上传到服务器,为此我使用 ASIHTTPRequest。我设置了一个循环来上传七个文件。但是在只上传最后一个文件之后,有人可以指出我哪里出错了。
我使用以下代码进行上传:
for (int i=1; i<8; i++)
{
NSString* filename = [NSString stringWithFormat:@"Photo%d.jpg", i];
NSString *path = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:filename];
[request setFile:path forKey:[NSString stringWithFormat:@"file"]];
}
[request startAsynchronous];
[resultView setText:@"Uploading data..."];
My Php file code is as following :
<?php
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
}
else
{
echo "Upload: " . $_FILES["file"]["name"] . "<br />";
echo "Type: " . $_FILES["file"]["type"] . "<br />";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";
if (file_exists("vinay/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"vinay/" . $_FILES["file"]["name"]);
echo "Stored in: " . "http://serverpath" . $_FILES["file"]["name"];
}
}
?>
最佳答案
您正在覆盖名为 file 的 key ,并且您需要使用队列。
做
[self setNetworkQueue:[ASINetworkQueue queue]];
[[self networkQueue] setDelegate:self];
for (int i=1; i<8; i++)
{
NSString* filename = [NSString stringWithFormat:@"Photo%d.jpg", i];
NSString *path = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:filename];
[request setFile:path forKey:[NSString stringWithFormat:@"file%d", i]];
[[self networkQueue] addOperation:request];
}
[[self networkQueue] go];
关于ios4 - 在iPhone中使用ASIHTTPRequest将图像上传到服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4680503/