gpt4 book ai didi

ios - 为什么在使用 AFNetworking 库将 wave 文件上传到服务器时应该得到二进制响应

转载 作者:行者123 更新时间:2023-11-29 10:52:50 29 4
gpt4 key购买 nike

我有一个要求,我在服务器上上传一个 wave 文件,然后服务器给我返回该文件的 URL。

我用过这个代码..

- (void)callUploadWS
{
NSString *vidURL = [[NSBundle mainBundle] pathForResource:@"alarm" ofType:@"wav"];
NSData *videoData = [NSData dataWithContentsOfURL:[NSURL fileURLWithPath:vidURL]];

NSURL *url = [NSURL URLWithString:@"http://6.6.237.190"];
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url];
// NSData *imageData = UIImageJPEGRepresentation([UIImage imageNamed:@"Romantic.jpg"], 0.5);
NSMutableURLRequest *request = [httpClient multipartFormRequestWithMethod:@"POST" path:@"/recorder/convert.php" parameters:nil constructingBodyWithBlock: ^(id <AFMultipartFormData>formData) {
[formData appendPartWithFileData:videoData name:@"file" fileName:@"speech.wav" mimeType:@"audio/vnd.wave"];
}];

AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[operation setUploadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {
NSLog(@"Sent %lld of %lld bytes", totalBytesWritten, totalBytesExpectedToWrite);
}];
// [httpClient enqueueHTTPRequestOperation:operation];

[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {NSLog(@"Success : %@",responseObject);}
failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"error: %@", operation.responseString);

NSLog(@"error: %@", error.userInfo);
}];
[operation start];
}

它正在上传文件并给我这个回复...

Success : <7b226669 6c65223a 22687474 703a5c2f 5c2f352e 392e3233 372e3135 305c2f72 65636f72 6465725c 2f75706c 6f61645c 2f353237 32336564 32366531 63662e77 6176227d>

但它应该给出这样的响应..

{
file: "http://6.6.237.190/recorder/upload/52723ef52972d.wav"
}

为什么这会给出二进制响应?

最佳答案

你必须像这样将 responseObject 转换为 NSString

NSString *responseString=[NSString stringWithUTF8String:[responseObject bytes]];

关于ios - 为什么在使用 AFNetworking 库将 wave 文件上传到服务器时应该得到二进制响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19705417/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com