how to upload an audio file using HTTP POST from i

2020-02-24 05:09发布

问题:

I am trying to upload an audio file in "caf" format from the iPhone to a web server. The used codes are given below. The problem is, I am not getting any file to upload, there is not output for the file names in PHP echo ! Any help would be greatly appreciated.

The code I am using at the iPhone end is:

NSData *fileData=[NSData dataWithContentsOfURL:recordedTmpFile];
NSURL *url=[NSURL URLWithString:@"http://somelink.com/upload.php"];
NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"POST"];

NSString *boundary = [NSString stringWithString:@"---------------------------14737809831466499882746641449"];
NSString *contentType = [NSString stringWithFormat:@"audio/x-caf; boundary=%@",boundary];
[request setValue:contentType forHTTPHeaderField: @"Content-Type"];

NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"userfile\"; filename=\"%@\"\r\n",[recordedTmpFile lastPathComponent]] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"Content-Type: audio/x-caf\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:fileData];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:body];

NSError *errr=nil;
NSURLResponse *resp=nil;
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:&resp error:&errr];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];

And the PHP code is:

echo "start";
echo "1".$_FILES['userfile']['name'];
echo "2".$_FILES['userfile']['type'];
echo "end";

Thanks.

- ahsan

Edit #1 :

using @Gypsa 's edit, I get an error. Code and Error message follows:

Code:

NSString *urlString = @"http://somelink/upload.php";
NSURL *url=[NSURL URLWithString:[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
ASIFormDataRequest *request=[ASIFormDataRequest requestWithURL:url];
NSLog(@"3333");
//NSString *
[request addFile:recordedTmpFile forKey:@"userfile"];
[request setRequestMethod:@"POST"];
//[request setDelegate:self];
NSLog(@"555");
[request setTimeOutSeconds:500];
[request startSynchronous];
NSLog(@"666");
NSLog(@"responseStatusCode %i",[request responseStatusCode]);
NSLog(@"responseString %@",[request responseString]);

Error Message:

2011-06-01 07:06:10.819 viewer1[16473:207] -[NSURL length]: unrecognized selector sent to    instance 0x5644900
2011-06-01 07:06:10.821 viewer1[16473:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSURL length]: unrecognized selector sent to instance 0x5644900'
   terminate called after throwing an instance of 'NSException'

Edit #2: Looks like I needed to make the address of the audio file from NSURL to NSString which I did :

NSString urlString=[url absoluteString];

However, now I am not getting any response from my php page..the response is null :(

Edit #3:

The error message I get reads :

responseError Error Domain=ASIHTTPRequestErrorDomain Code=6 "No file exists at file://localhost/var/folders/45/45y8AmvjHvyUUl0mCOpmC++++TI/-Tmp-/328620103851.caf" UserInfo=0x534e500 {NSLocalizedDescription=No file exists at file://localhost/var/folders/45/45y8AmvjHvyUUl0mCOpmC++++TI/-Tmp-/328620103851.caf}

The response status code is 0.

Edit #4:

Solved !!! See the answer :)

回答1:

I have used this code for video upload, you can use it for your audio upload.

NSString *str = [NSString stringWithFormat:@"%@/uploadVideoIphone.php",appUrl];
NSURL *url = [NSURL URLWithString:[str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];      
NSString *filePath = [urlvideo path];
[request addFile:filePath forKey:@"uploadfile"];                        
[request setRequestMethod:@"POST"];
[request setDelegate:self];
[request setTimeOutSeconds:500];
[request startSynchronous];
NSLog(@"responseStatusCode %i",[request responseStatusCode]);
NSLog(@"responseString %@",[request responseString]);