使用ASIHTTPRequest上传txt文件

时间:2012-04-09 06:46:53

标签: iphone objective-c ios asihttprequest

我在将数据文件上传到服务器时遇到问题。在Objective-c方面,一切似乎都在起作用(我启用了一个缓慢增加到100%的进度条)。这是代码(您可以假设uploadURL是正确的URL,outFilePath是文件“out.txt”的正确路径,我已经加倍检查了这一点):

ASIFormDataRequest *uploadRequest = [ASIFormDataRequest requestWithURL:uploadURL];
[uploadRequest setUploadProgressDelegate:uploadProgress];
[uploadRequest setFile:outFilePath forKey:@"file"];
[uploadRequest startSynchronous];

服务器似乎没有收到数据文件。这是php代码(我没写过):

<?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 />";
move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $_FILES["file"]$
echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
}
?>

任何关于我做错事的想法都会受到赞赏。谢谢!

1 个答案:

答案 0 :(得分:2)

如果我运行此代码(取自ASIHTTP示例)

ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:@"http://buildmac.ee.pitt.edu/~wsn/boinclite/upload_file"];

[request setPostValue:@"test" forKey:@"value1"];
[request setPostValue:@"test" forKey:@"value2"];
[request setPostValue:@"test" forKey:@"value3"];
[request setTimeOutSeconds:20];

//Create a 256KB file
NSData *data = [[[NSMutableData alloc] initWithLength:256*1024] autorelease];
NSString *path = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"file"];
[data writeToFile:path atomically:NO];

[request setFile:path forKey:@"file"];

[request startAsynchronous];

我得到了这个结果(使用Charles Proxy来监控上传):

Upload: file
Type: application/octet-stream
Size: 256 Kb
Temp file: /private/var/tmp/phpi6UDp1
Stored in: upload/file

所以一切都在运行。