Upload .mp3 file to PHP server in Objective-C

0

How do I upload an .mp3 file on my iphone to my server using an upload page I was able to do with more images I am not able to do with .mp3 files could someone help me? The following is 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}";
}
?>
    
asked by anonymous 25.09.2015 / 14:00

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 taking binary information from a JPEG Image. And now of 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 a detail, since the name of the file to be sent from ipodfile.jpg to file.mp3 has been changed, identifying it as an mp3 file.

Change:

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

To:

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

Why? Attaching the new audioData created.

    
25.09.2015 / 14:12