iOS   发布时间:2022-05-04  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了使用POST将文件从iOS发送到PHP大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

概述

我正在尝试将音频文件发送到php服务器.这是Objective-C代码: NSData *data = [NSData dataWithContentsOfFile:filePath]; NSLog(@"File Size: %i",[data length]); //set up request NSMutableURLrequest *request= [[NSMutableURLrequ
我正在尝试将音频文件发送到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 sethttpR_372_11845@ethod:@"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];

这是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 sethttpR_372_11845@ethod: @"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所遇到的程序开发问题。

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

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。