Here it is my code to upload an audio file
. I tried lot of questions and answers in stackoverflow
. But still i am not getting any improvement on this. I have to upload an audio file with a php link
. If i do that, i am getting response like "already exists
" always. I tried to change the filename and upload it. still getting same response. Actually i have to receive a response from the server as link of a file i uploaded. I am not having any knowledge about php
. Anyone help me with this. Any help would be appreciated to clear my issue.
NSData *fileData=[NSData dataWithContentsOfURL:soundFileURL];
NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"https://somesite.com/upload.php"]];
[request setHTTPMethod:@"POST"];
NSString *boundary = [NSString stringWithString:@"---------------------------14737809831466499882746641449"];
NSMutableData *body = [[NSMutableData alloc]init];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"userfile\"; filename=\"fvgv4r346r4r4h3ur543ty5u54y5u4574545g4g5.samr\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
//[soundFileURL lastPathComponent]
[body appendData:[[NSString stringWithString:@"Content-Type: music/samr\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:fileData];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:body];
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];
// NSLog(@"%@",[NSString stringWithUTF8String:[body bytes]]);
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
NSLog(@"%@",returnString);