上传图像到服务器使用ASIHTTPRequest在iPhone
问题描述:
我必须上传图像到服务器形式iPhone我使用ASIHTTPRequest。我已经设置了一个循环来上传七个文件。但是在执行完最后一个文件之后,有人可以指出我错误的地方。上传图像到服务器使用ASIHTTPRequest在iPhone
我使用下面的代码上传:
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"];
}
}
?>
答
您正在覆盖名为文件&的密钥,您需要使用队列。
做
[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];
答
你覆盖request.file
,因此只有最后一个被上传。您必须为每个文件提出个别请求。
或者您可以使用[request addFile:(id)data withFileName:(NSString *)fileName andContentType:(NSString *)contentType forKey:(NSString *)key]
在一个请求中发送多个文件。
+0
我要上传多个文件(文件数未证实)现在怎么设置的要求,使我们可以采用这种带环。 – 2011-01-13 13:31:51
嗨我试过你的选择,但这次没有上传文件。如果这造成了一些问题,我还附加了我的PHP文件代码。 – 2011-01-13 13:51:16