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;
}
有人能幫助我嗎?非常感謝!
非常感謝您的幫助!這讓我瘋狂!我不會在第一次發佈HTML時看到需要什麼。 – Adrideh 2014-09-13 23:47:48