1

I am using this code for audio file upload from iphone app to server.

 NSFileManager *fileManager = [[NSFileManager alloc] init];
    NSDictionary *fileAttributes = [fileManager attributesOfItemAtPath:mediaPath error: NULL];

    NSLog(@"%@",fileAttributes);
    if (fileAttributes != nil) {

        NSNumber *fileSize;
        fileSize = [fileAttributes objectForKey:NSFileSize];
        NSLog(@"%@",fileSize);

       // NSData *audioData = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:audioData ofType:@""]];

        NSString *urlString = [NSString stringWithFormat:@"%@audioupload.php",mydomainurl];


        NSLog(@"%@",audioData);
        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=\".caf\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[NSData dataWithData:audioData]];
        [body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
        [request setHTTPBody:body];

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

        NSLog(@"%@",returnString);

Doing so it's converting the .caf file to binary and taking too much (1-2 mints) time to upload audio in server. Not only this the file size is getting bigger in server. I have included

ASIFormDataRequest 

But don't know how to use this in my code. Can anyone suggest me that how can i reduce the upload time.

But

3
  • What sizes are the files on the iPhone and the server? Is it possible that the upload time is reasonable because of the size of the data? Commented Nov 29, 2012 at 14:42
  • NSFileSize = 526300; And upload file size in server is 1mb. It's 3 seconds audio.. :( Commented Nov 29, 2012 at 14:50
  • Can you use something like Fiddler to examine the size of your request to ensure that you are sending what you think that you are sending? Regarding the file size on the server, is your server side code appending to an existing file instead of creating a new one? Commented Nov 29, 2012 at 16:05

1 Answer 1

1

I have an app does this very quickly and my code is very similar to yours.

The only difference is on the line:

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

I have a file name, not just the .caf extension:

[body appendData:[@"Content-Disposition: attachment; name=\"file2\"; filename=\"test.caf\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];

Also, after you append the audio data to the body with this line:

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

and before you close the form with this line:

[body appendData:[[NSString stringWithFormat:@"--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];

I have this line:

[body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];

I hope this works for you. If not, you should check your server side code because you and I have practically the same code and mine uploads audio very quickly.

Sign up to request clarification or add additional context in comments.

2 Comments

$uploaddir = './'; //Uploading to same directory as PHP file print_r($_FILES['userfile']); $file = basename($_FILES['userfile']['name']); $uploadFile = $file; $randomNumber = rand(0, 99999); $newName = $uploadDir . $randomNumber . $uploadFile; if (is_uploaded_file($_FILES['userfile']['tmp_name'])) { echo "Temp file uploaded. \r\n"; } else { echo "Temp file not uploaded. \r\n"; } This is my server side code friend
Ok. Did you added a file name and the line that appends @"\r\n" before closing the form? Here is how my server side code works: $base_url = 'xxx.xxx.xxx.xxx'; $upload_dir = "uploads"; if( isset($_FILES["file"]) ){ $image_name = basename($_FILES["file"]["name"]); $image_name = time() . '_' . basename($_FILES["file"]["name"]); $image_name = str_replace(' ', '_', $image_name); $filename = getcwd() . '/' . $upload_dir . '/' . $image_name; } if( move_uploaded_file($_FILES["file"]["tmp_name"], $filename) ) { //Success! $file_url = $base_url . '/' . $upload_dir . '/' . $image_name; }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.