Upload. mp3 file to PHP server in Objective-C

Asked

Viewed 182 times

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}";
}
?>

1 answer

2

Change the image encoding from the top:

NSData *imageData = UIImageJPEGRepresentation(imgFoto.image, 90);

For generic data, in this case audio:

NSString *audioFilePath = @""; // caminho local de onde o arquivo está armazenado
NSData *audioData = [[NSData alloc] initWithContentsOfFile:audioFilePath];

Why? As the function name pointed out, you were picking binary information from a JPEG Image. And now any kind of binary file (audio, video, etc).

Change the line:

[body appendData:[@"Content-Disposition: form-data; name=\"userfile\"; filename=\"ipodfile.jpg\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];

To:

[body appendData:[@"Content-Disposition: form-data; name=\"userfile\"; filename=\"file.mp3\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];

Why? Just one detail, since the file name has been changed from ipodfile.jpg to file.mp3, identifying that it is an mp3 file.

Change:

[body appendData:[NSData dataWithData:imageData]];//imageData

To:

[body appendData:[NSData dataWithData:audioData]];

Why? Attaching the new audioData created.

  • Thank you very much Alfred Baudisch, it worked perfect broke me a branch.

  • 1

    Good @Brunorichart. I only ask you to mark my answer as the chosen one, so your question is completed and answered.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.