使用POST将文件从iOS发送到PHP

使用POST将文件从iOS发送到PHP,第1张

概述我正在尝试将音频文件发送到PHP服务器.这是Objective-C代码: NSData *data = [NSData dataWithContentsOfFile:filePath];NSLog(@"File Size: %i",[data length]);//set up requestNSMutableURLRequest *request= [[NSMutableURLRequ 我正在尝试将音频文件发送到PHP服务器.这是Objective-C代码:

NSData *data = [NSData dataWithContentsOffile:filePath];NSLog(@"file Size: %i",[data length]);//set up requestNSMutableURLRequest *request= [[NSMutableURLRequest alloc] init];[request setURL:[NSURL URLWithString:urlString]];[request sethttpMethod:@"POST"];//required xtra infoNsstring *boundary = @"---------------------------14737809831466499882746641449";Nsstring *ContentType = [Nsstring stringWithFormat:@"multipart/form-data; boundary=%@",boundary];[request addValue:ContentType forhttpheaderFIEld: @"Content-Type"];//body of the postNSMutableData *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];

这是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代码中注释掉两行,以确保您至少可以获得服务器端的名称和文件名的纯文本.

祝你好运!

总结

以上是内存溢出为你收集整理的使用POST将文件从iOS发送到PHP全部内容,希望文章能够帮你解决使用POST将文件从iOS发送到PHP所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1067448.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-26
下一篇2022-05-26

发表评论

登录后才能评论

评论列表(0条)

    保存