How to upload an audio file to godaddy server from iphone application?

I made an iphone application to download an audio file.

I also made webervices on asp.net to support other functions for this application and published it on godaddy server.

I want to download / save an audio file on this server from my iphone application.

I searched for a lot of codes, but they are not relevant and useful.

So how can I do this?

How to get the server path where we can upload the file?

Please provide an example iphone code for this.

+1
source share
1 answer

This is how I send a file (can be any file, image, video, sound) to a web service

NSString *fileName = @"myAudio";
    NSData *audioFile = [NSData dataWithContentsOfFile:@"myAudio.ext"]; //some audio
    NSString *urlString = @"http://www.something.com/someservice";
    urlString = [urlString stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];
    NSURL *url = [NSURL URLWithString:urlString];
    NSString *strPostLength = [NSString stringWithFormat:@"%d", [audioFile length]];  
    NSMutableURLRequest *uRequest = [[NSMutableURLRequest alloc] init];  
    [uRequest setURL:url];  
    [uRequest setHTTPMethod:@"POST"];  
    if (audioFile)
    {
        NSMutableData *body = [NSMutableData data];

        NSString *boundary = [NSString stringWithString:@"---------------------------14737809831466499882746641449"];
        NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
        [uRequest addValue:contentType forHTTPHeaderField: @"Content-Type"];

        /*
         now lets create the body of the post
         */

        [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
        [uRequest setValue:strPostLength forHTTPHeaderField:@"Content-Length"];  
        [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"userfile\"; filename=\"%@.ext\"\r\n",fileName] dataUsingEncoding:NSUTF8StringEncoding]];

// userfile → the variable name specified in the server code ...

        [body appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[NSData dataWithData:audioFile]];
        [body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];

        // setting the body of the post to the reqeust
        [uRequest setHTTPBody:body];
    }

    NSURLConnection *uConn=[[NSURLConnection alloc] initWithRequest:uRequest delegate:self];  
    if (uConn)   
    {  
        NSLog(@"Failed to connect to url"); 
    } 
    uRequest = nil;

I hope this helps you solve your problem ...

+3
source

All Articles