0
How do I upload a file. mp3 on my iphone to my server using an upload page I managed to do with images but I’m not able to do with files . mp3 could someone help me? Follow the below the code I use:
Objective-C:
NSData *imageData = UIImageJPEGRepresentation(imgFoto.image, 90);
NSString *urlString = @"http://127.0.0.1:8888/uploadsom/upload.php";
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init] ;
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];
NSString *boundary = @"---------------------------14737809831466499882746641449"
;
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];
NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Disposition: form-data; name=\"userfile\"; filename=\"ipodfile.jpg\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:imageData]];//imageData
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:body];
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
NSLog(@"%@",returnString);
PHP page
<?php
$uploaddir = './uploads/';
$file = basename($_FILES['userfile']['name']); $uploadfile = $uploaddir . $file;
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
echo "http://meusite.com.br/uploads/{$file}";
}
?>
Thank you very much Alfred Baudisch, it worked perfect broke me a branch.
– Bruno Richart
Good @Brunorichart. I only ask you to mark my answer as the chosen one, so your question is completed and answered.
– AlfredBaudisch