2

请任何人知道如何发送请求并在 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]; 
4

2 回答 2

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 对象的内容添加到请求中,清除任何其他具有相同 key 的内容,您可以指定 content-type 和文件名

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

也许您可以验证请求是否成功

if ([request responseStatusCode] == 200) {
            NSLog (@"Response status code: %i", [request responseStatusCode]); }
于 2012-04-21T14:29:02.847 回答
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");

 }
于 2013-05-17T06:17:18.667 回答