1

I have made an iphone app to upload an audio file.

I have also made webervices in asp.net to support other functionality for this app and published it on godaddy server.

I want to upload/save an audio file on that server from my iphone app.

I have searched many codes but they are not relevant and usefull.

So how can i do this ?

How to get serverpath where we can upload the file?

Please provide some sample code for iphone to do this.

Rohan
  • 2,939
  • 5
  • 36
  • 65
  • 1
    It doesn't become a better question by deleting and reposting. http://stackoverflow.com/questions/10749777/how-to-upload-audio-file-on-godaddy-server – jscs May 25 '12 at 07:23
  • @JacquesCousteau- i have not reposted the question. What is the problem? – Rohan May 25 '12 at 07:27
  • 1
    Are you using SOAP or Restful services..? – Krrish May 25 '12 at 07:27
  • @Krrish- thaks for reply. I am not using SOAP And do not have any idea about Restful service. : ( – Rohan May 25 '12 at 07:30

1 Answers1

3

This is how I POST a file (can be any file , image, video, audio anything) 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 -> variable name given 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 will help you in solving your issue...

Krrish
  • 2,256
  • 18
  • 21
  • thanks a lot to reply. What is the "someservice" in url? And what is the servercode? – Rohan May 25 '12 at 07:47
  • Rohan, thats some dummy text, you can replace the url, filename etc with the original texts which you are planning to use – Krrish May 25 '12 at 07:50
  • Ok. but should i required any servercode to do this thing? – Rohan May 25 '12 at 08:58
  • Yes, You need sever code for this but I dont have good knowledge on sever side programming and anything I say about it could miss lead you. I dont want that to happen. Please accept my answer ;) – Krrish May 25 '12 at 10:41
  • What should be the content type if I'm posting NSData that is made from NSString? – Satyam Mar 10 '14 at 13:57