从iOS应用程序上传文件到服务器

时间:2013-10-30 04:54:48

标签: ios file-upload upload

我可以在iOS应用程序上将图像以外的文件上传到服务器吗?我使用UIImagePickerController,它允许选择图像或拍照,然后我附加它们并上传到服务器。我可以用iPhone中的word或pdf文件做同样的事情 拜托,帮助我!

1 个答案:

答案 0 :(得分:1)

以下是ASIHTTP图书馆使用的代码 ASIHTTP Library Download

ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:@"Your Url"];

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

[request setTimeOutSeconds:20];

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

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

[request startAsynchronous];

假设您使用的是PHP服务器,那么下面的代码可能会对您有所帮助

$filedir = '';
$uploadfile = $filedir.basename($_FILES['myfile']['name']);


move_uploaded_file($_FILES['myfile']['tmp_name'], $uploadfile);