ASIHTTP请求

时间:2012-04-21 13:57:36

标签: iphone

请任何人知道如何发送请求并在ASIHTTP中发送带有一些参数的文件我的代码如下:我无法发送文件

NSURL *audiourl = [NSURL URLWithString:@"http://dev.kuwait.tt/demo_radioapps/radio_mobile/adduserrequest.php?program_id=3&user_name=abhishek&user_email_id=abhi@tets.com&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]; 

2 个答案:

答案 0 :(得分: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对象的内容添加到请求中,使用相同的密钥清除任何其他对象 并且您可以指定内容类型和文件名

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

也许您可以使用

验证请求是否成功
if ([request responseStatusCode] == 200) {
            NSLog (@"Response status code: %i", [request responseStatusCode]); }

答案 1 :(得分: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");

 }