2014-09-13 165 views
0

我想通過POST請求發送avaudiorecorder到服務器。但由於某些原因,我無法解決,因此無法上傳。 我首先採用我想發送的文件的路徑,然後創建一個包含所有需要的標題的Post請求方法。服務器的響應是沒有文件上傳,所以我想我做的文件路徑有問題,但我沒有得到解決方案。 我需要發送帶有授權內容的POST請求,如顯示的那樣,帶有多部分表單數據和邊界的內容類型以及作爲「uri_file」和文件上載的文件。 {uri_file:文件} 我嘗試發送,但服務器響應 - NotEnoughData = 「你必須提供一個文件」; -AVaudioRecorder發佈請求iOS沒有發送

........... 

-(NSData *)sendTrack{ 

NSMutableDictionary *dictrvo = [NSMutableDictionary dictionaryWithObjectsAndKeys: audioRecorder.url,@"uri_file", nil ]; 
NSUserDefaults *prefs= [NSUserDefaults standardUserDefaults]; 


// get the audio data from main bundle directly into NSData object 
NSURL *filePath = [prefs objectForKey:@"Revo1"]; 
if (filePath == nil) { 
    NSLog(@"vacio1"); 
} 
NSData *result =[NSKeyedArchiver archivedDataWithRootObject:dictrvo]; 
NSData *archivo = [[NSData alloc] initWithContentsOfFile:filePath]; 
if (result == nil) { 
    NSLog(@"vacio"); 
} 

NSData *response= [self createTrackPostRequest:archivo]; 
NSDictionary *dictionaryResponse = [self getDataFromJson:response]; 

return nil; 

} 

// POST請求方法

-(NSData *)createTrackPostRequest:(NSData *)requestData{ 

NSString *strData = [[NSString alloc]initWithData:requestData encoding:NSUTF8StringEncoding]; 
// Print out new string 
NSLog(@"DATA: %@",strData); 
//URL 
NSURL *URL = kRevoTrack; 
if(URL == nil) 
    NSLog(@"estoy vacio"); 

//REQUEST 
NSString *username; 
NSString *api_key; 
NSMutableURLRequest *request= [NSMutableURLRequest requestWithURL:URL cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60]; 
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults]; 
username = [prefs objectForKey:@"userName"]; 
api_key = [prefs objectForKey:@"api_key"]; 
NSString * authorizationcontent = [@"ApiKey " stringByAppendingString:[username stringByAppendingString:[@":" stringByAppendingString:api_key]]]; 
[request setHTTPMethod:@"POST"]; 
[request setValue:authorizationcontent forHTTPHeaderField:@"Authorization"]; 
[request setValue:@"multipart/form-data ; boundary = ---------1239821390213hdjfhsakdasjl123ad" forHTTPHeaderField:@"Content-Type"]; 
[request setValue:@"form-data; name =\"uri_file\"; file =\"MyAudioMemo.m4a\"\r\n" forHTTPHeaderField:@"Content-Disposition"]; 
dataUsingEncoding:NSUTF8StringEncoding]]; 

[request setValue: [NSString stringWithFormat: @"%d", [requestData length]] forHTTPHeaderField:@"Content-Length"]; 
[request setHTTPBody:requestData]; 

NSURLResponse *response = nil; 
NSError *error = nil; 

NSData *result = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]; 

if(error == nil){ 

    // REQUEST SENT WITHOUT ERROR 
    return result; 

}else{ 

    //ERROR SENDING THE REQUEST 

    NSLog(@"ERROR"); 
} 
return nil; 
} 

有人能幫助我嗎?非常感謝!

回答

0

你只需要格式化請求有點不同。在創建多部分請求時,我喜歡先用HTML創建表單。然後,我發佈Firefox中的示例HTML並啓用了Firebug。然後我可以分析發佈的數據並確保我的請求正文匹配。

-----------------------------173920846362415155847887193 
Content-Disposition: form-data; name="uri_file"; filename="MyAudioMemo.m4a" 
Content-Type: audio/x-m4a 
<bunch of data> 
-----------------------------173920846362415155847887193-- 

這裏是修改的請求方法。

- (NSData *)createTrackPostRequest:(NSData *)requestData 
{ 
    // Prepare authorization 
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; 
    NSString *username = [defaults objectForKey:@"userName"]; 
    NSString *api_key = [defaults objectForKey:@"api_key"]; 
    NSString *authorizationcontent = [@"ApiKey " stringByAppendingString:[username stringByAppendingString:[@":" stringByAppendingString:api_key]]]; 

    // Create the request 
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init]; 
    [request setURL:kRevoTrack]; 
    [request setHTTPMethod:@"POST"]; 
    [request setValue:authorizationcontent forHTTPHeaderField:@"Authorization"]; 
    [request setValue:[NSString stringWithFormat: @"%d", [requestData length]] forHTTPHeaderField:@"Content-Length"]; 
    [request addValue:@"multipart/form-data; boundary=---------------------------725025452680558686651931680" forHTTPHeaderField:@"Content-Type"]; 

    // Create the body 
    NSMutableData *body = [NSMutableData data]; 
    [body appendData:[@"\r\n-----------------------------725025452680558686651931680\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; 
    [body appendData:[@"Content-Disposition: form-data; name=\"uri_file\"; filename=\"MyAudioMemo.m4a\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; 
    [body appendData:[@"Content-Type: audio/x-m4a\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; 
    [body appendData:requestData]; 
    [body appendData:[@"\r\n-----------------------------725025452680558686651931680--" dataUsingEncoding:NSUTF8StringEncoding]]; 
    [request setHTTPBody:body]; 

    // Send the request 
    NSURLResponse *response = nil; 
    NSError *error = nil; 
    NSData *result = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]; 

    // Return the result 
    if (error == nil) { 
     NSLog(@"result: %@", [[NSString alloc] initWithData:result encoding:NSUTF8StringEncoding]); 
     return result; // REQUEST SENT WITHOUT ERROR 
    } else { 
     NSLog(@"ERROR"); // ERROR SENDING THE REQUEST 
    } 
    return nil; 
} 
+0

非常感謝您的幫助!這讓我瘋狂!我不會在第一次發佈HTML時看到需要什麼。 – Adrideh 2014-09-13 23:47:48