2012-05-25 63 views
1

我做了一個iPhone應用程序來上傳音頻文件。如何從iphone應用上傳godaddy服務器上的音頻文件?

我也在asp.net中使webervices支持此應用程序的其他功能,並將它發佈到godaddy服務器上。

我想從我的iPhone應用程序上傳/保存該服務器上的音頻文件。

我已經搜索了許多代碼,但它們不相關並且非常有用。

那麼我該如何做到這一點?

如何獲取我們可以上傳文件的serverpath?

請爲iPhone提供一些示例代碼。

+1

它不會成爲通過刪除並重新發佈一個更好的問題。 http://stackoverflow.com/questions/10749777/how-to-upload-audio-file-on-godaddy-server –

+0

@ JacquesCousteau-我還沒有轉貼的問題。問題是什麼? – Rohan

+1

你使用SOAP還是Restful服務..? – Krrish

回答

3

這是我張貼的文件(可以是任何文件,圖像,視頻,音頻的任何東西)到web服務

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的 - >變量名在服務器代碼中給出.. 。

 [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; 

我希望這將幫助您解決問題...

+0

非常感謝您回覆。什麼是url中的「someservice」?什麼是服務器代碼? – Rohan

+0

Rohan,這是一些虛擬文本,您可以用您打算使用的原始文本替換url,文件名等 – Krrish

+0

好的,但我應該要求任何服務器代碼來做這件事嗎? – Rohan

相關問題