2012-04-21 85 views
2

請任何一個知道abhout如何發送請求,並在ASIHTTP發送文件的一些參數,我的代碼是波紋管我不能夠將文件發送ASIHTTP請求

NSURL *audiourl = [NSURL URLWithString:@"http://dev.kuwait.tt/demo_radioapps/radio_mobile/adduserrequest.php?program_id=3&user_name=abhishek&[email protected]&audiofile=review.caf&mode=AddRequest"]; 
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:audiourl]; 
[request addData:tempData withFileName:@"review.caf" andContentType:@"audio/caf" forKey:@"audiofile"]; 

[request setRequestMethod:@"POST"]; 

[request setDelegate:self]; 

[request startSynchronous]; 
+1

這是不好的形式發送請求同步。您應該異步發送它以防止UI被鎖定。 – jmstone617 2012-04-21 16:31:52

回答

0

你嘗試過:[request setData:tempData withFileName:@"review.caf" andContentType:@"audio/caf" forKey:@"audiofile"];

//一個NSData對象的內容添加到該請求,並可以指定內容類型和文件名

- (void)addData:(id)data withFileName:(NSString *)fileName andContentType:(NSString *)contentType forKey:(NSString *)key 

//添加一個NSData對象的內容請求,CLE aring任何其他有同樣鍵 ,你可以指定內容類型和文件名

- (void)setData:(id)data withFileName:(NSString *)fileName andContentType:(NSString *)contentType forKey:(NSString *)key; 

也許你可以用

if ([request responseStatusCode] == 200) { 
      NSLog (@"Response status code: %i", [request responseStatusCode]); } 
0

下面驗證請求的成功是一個工作的功能,我在我的應用程序中使用,請檢查是否有幫助:

- (void)uploadAudioFile:(NSString *)audioPath{ 
     NSURL *audiourl=[NSURL URLWithString:audioPath]; 
    NSData *audioData=[NSData dataWithContentsOfFile:audiourl.path]; 
      @try { 
     NSString *strURL = @"http://www.abc.com/data/default.aspx"; 
     ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:strURL]]; 
     request= [request initWithURL:[NSURL URLWithString:strURL]]; 
     [request setShouldContinueWhenAppEntersBackground:YES]; 
     [request setShouldAttemptPersistentConnection:YES]; 
     [request setPostValue:[Settings UserName] forKey:@"lblUsername"]; 
     [request setPostValue:[Settings Password] forKey:@"lblpassword"]; 
     [request addData:audioData withFileName:@"audio.caf" andContentType:@"audio/x-caf" forKey:@"filMyFile"]; 

     [request setCompletionBlock:^{ 
      NSLog(@"Request Completed");    
      [self requestFinished:request]; 
     }]; 

     //if request failed 
     [request setFailedBlock:^{ 
      [self requestFailed:request]; 
      NSLog(@"request Failed: %@",[request error]); 
     }]; 
     [request startAsynchronous]; 
    } 
    @catch (NSException *exception) { 
     NSLog(@"Exception name and reson is %@ ------- %@",exception.name, exception.reason); 
    } 
    @finally { 
     NSLog(@"finalyy of upload audio"); 
    } 
} 


- (void)requestFinished:(ASIHTTPRequest *)request {  
    NSLog(@"requestFinished"); 

} 

- (void)requestFailed:(ASIHTTPRequest *)request { 
     NSLog(@"requestFailed"); 

}