使用POST将文件从iOS发送到PHP
发布时间:2020-12-15 01:46:05 所属栏目:百科 来源:网络整理
导读:我正在尝试将音频文件发送到PHP服务器.这是Objective-C代码: NSData *data = [NSData dataWithContentsOfFile:filePath];NSLog(@"File Size: %i",[data length]);//set up requestNSMutableURLRequest *request= [[NSMutableURLRequest alloc] init];[reque
我正在尝试将音频文件发送到PHP服务器.这是Objective-C代码:
NSData *data = [NSData dataWithContentsOfFile:filePath]; NSLog(@"File Size: %i",[data length]); //set up request NSMutableURLRequest *request= [[NSMutableURLRequest alloc] init]; [request setURL:[NSURL URLWithString:urlString]]; [request setHTTPMethod:@"POST"]; //required xtra info NSString *boundary = @"---------------------------14737809831466499882746641449"; NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary]; [request addValue:contentType forHTTPHeaderField: @"Content-Type"]; //body of the post NSMutableData *postbody = [NSMutableData data]; [postbody appendData:[[NSString stringWithFormat:@"rn--%@--rn",boundary] dataUsingEncoding:NSUTF8StringEncoding]]; [postbody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name="thefile"; filename="recording"rn"] dataUsingEncoding:NSUTF8StringEncoding]]; [postbody appendData:[[NSString stringWithString:@"Content-Type: application/octet-streamrn"] dataUsingEncoding:NSUTF8StringEncoding]]; [postbody appendData:data]; [postbody appendData:[[NSString stringWithFormat:@"rn--%@--rn",boundary] dataUsingEncoding:NSUTF8StringEncoding]]; [request setHTTPBody:postbody]; apiConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self]; 这是PHP代码: if (is_uploaded_file($_FILES['thefile']['tmp_name'])) { echo "FILE NAME: ".$_FILES['thefile']['name']; } else { echo "Nothing"; } 文件大小取决于录制的音频长度,但有数据. 我得到了“没什么”的回应. 解决方法
要调试:在PHP端,尝试:
$file = $_POST['name']; echo $file 尝试使用以下代码段代替当前代码.将URL(www.yourWebAddress.com)和脚本名称更改为适合您问题的值. NSData *data = [NSData dataWithContentsOfFile:filePath]; NSMutableString *urlString = [[NSMutableString alloc] initWithFormat:@"name=thefile&&filename=recording"]; [urlString appendFormat:@"%@",data]; NSData *postData = [urlString dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]; NSString *postLength = [NSString stringWithFormat:@"%d",[postData length]]; NSString *baseurl = @"https://www.yourWebAddress.com/yourServerScript.php"; NSURL *url = [NSURL URLWithString:baseurl]; NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:url]; [urlRequest setHTTPMethod: @"POST"]; [urlRequest setValue:postLength forHTTPHeaderField:@"Content-Length"]; [urlRequest setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"]; [urlRequest setHTTPBody:postData]; NSURLConnection *connection = [NSURLConnection connectionWithRequest:urlRequest delegate:self]; [connection start]; NSLog(@"Started!"); 在服务器端,我有以下代码: <?php $name = $_POST['name']; $filename = $_POST['filename']; echo "Name = '" . $name . "',filename = '" . $filename . "'."; error_log ( "Name = '" . $name . "',filename = '" . $filename . "'." ); ?> 我收到了以下输出: [23-May-2012 11:56:18] Name =’thefile’,filename =’recording’. 我不知道为什么它不适合你.你必须缺少一步.尝试在上面引用“数据”的url POST代码中注释掉两行,以确保您至少可以获得服务器端的名称和文件名的纯文本. 祝你好运! (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |