Chr*_*ris 4 php iphone xcode objective-c
我正在尝试将音频文件发送到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:@"\r\n--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"thefile\"; filename=\"recording\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:data];
[postbody appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:postbody];
apiConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
Run Code Online (Sandbox Code Playgroud)
这是PHP代码:
if (is_uploaded_file($_FILES['thefile']['tmp_name'])) {
echo "FILE NAME: ".$_FILES['thefile']['name'];
} else {
echo "Nothing";
}
Run Code Online (Sandbox Code Playgroud)
文件大小取决于录制的音频长度,但有数据.
我收到了"没什么"的回应.
要调试:在PHP端,尝试:
$file = $_POST['name'];
echo $file
Run Code Online (Sandbox Code Playgroud)
尝试使用以下代码段代替当前代码.将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!");
Run Code Online (Sandbox Code Playgroud)
在服务器端,我有以下代码:
<?php
$name = $_POST['name'];
$filename = $_POST['filename'];
echo "Name = '" . $name . "', filename = '" . $filename . "'.";
error_log ( "Name = '" . $name . "', filename = '" . $filename . "'." );
?>
Run Code Online (Sandbox Code Playgroud)
我收到了以下输出:
[23-May-2012 11:56:18] Name ='thefile',filename ='recording'.
我不知道为什么它不适合你.你必须缺少一步.尝试在上面引用"数据"的url POST代码中注释掉两行,以确保您至少可以获得服务器端的名称和文件名的纯文本.
祝好运!
| 归档时间: |
|
| 查看次数: |
18887 次 |
| 最近记录: |