目前我正在将 xml 文件从 mac 上传到服务器。它工作正常。下面是代码:
osx代码
NSString *urlString1 = @"http://username:pwd@ip/files/upload.php";
NSMutableURLRequest *request1 = [[NSMutableURLRequest alloc] init];
[request1 setURL:[NSURL URLWithString:urlString1]];
[request1 setHTTPMethod:@"POST"];
NSString *boundary1 = @"---------------------------14737809831466499882746641449";
NSString *contentType1 = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary1];
[request1 addValue:contentType1 forHTTPHeaderField:@"Content-Type"];
NSMutableData *body1 = [NSMutableData data];
[body1 appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary1] dataUsingEncoding:NSUTF8StringEncoding]];
[body1 appendData:[@"Content-Disposition: form-data; name=\"userfile\"; filename=\"data.xml\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body1 appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body1 appendData:[NSData dataWithData:xmlData]];
[body1 appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n", boundary1] dataUsingEncoding:NSUTF8StringEncoding]];
[request1 setHTTPBody:body1];
NSLog(@"xml data %@",xmlData);
NSData *returnData1 = [NSURLConnection sendSynchronousRequest:request1 returningResponse:nil error:nil];
和PHP 脚本:
<?php
$target_path = "./";
$target_path = $target_path.'data.xml';
if(move_uploaded_file($_FILES['userfile']['tmp_name'],$target_path)) {
echo "The file has been uploaded";
} else{
echo "There was an error uploading the file, please try again!";
}
?>
现在我有一个案例,我应该添加多个文件。那么文件命名是如何工作的呢?任何人都请帮助我。