Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

您好,文件上传问题打扰了 #33

Open
Samuel-Xi opened this issue Aug 9, 2024 · 6 comments
Open

您好,文件上传问题打扰了 #33

Samuel-Xi opened this issue Aug 9, 2024 · 6 comments

Comments

@Samuel-Xi
Copy link

1723162198314
您好,像这种带参数的文件上传该怎么处理,我使用methodType=ZBMethodTypeUpload,或是methodType=ZBMethodTypePOST都报error: Error Domain=com.alamofire.error.serialization.response Code=-1011 "Request failed: bad request (400)" UserInfo={NSLocalizedDescription=Request failed: bad request (400) 错误,request我是这样配置的:
request.methodType=ZBMethodTypePOST;
request.requestSerializer = ZBJSONRequestSerializer;
request.responseSerializer = ZBJSONResponseSerializer;
request.parameters = @{@"submission_id":@"10"};
[request addFormDataWithName:@"file" fileURL:outFileUrl];
不知道是啥问题,帮忙看看,感激不尽🤝🤝

@Suzhibin
Copy link
Owner

Suzhibin commented Aug 9, 2024

request.requestSerializer = ZBHTTPRequestSerializer 试试 @Samuel-Xi

@Samuel-Xi
Copy link
Author

您好,这样设置了request.requestSerializer = ZBHTTPRequestSerializer,还是报相同的错误Error Domain=com.alamofire.error.serialization.response Code=-1011 "Request failed: bad request (400)" UserInfo={NSLocalizedDescription=Request failed: bad request (400)

@Suzhibin
Copy link
Owner

Suzhibin commented Aug 9, 2024

methodType=ZBMethodTypeUpload
request.requestSerializer 与 request.responseSerializer 互相组合 更换 多试试 @Samuel-Xi

@Samuel-Xi
Copy link
Author

这样写可以请求成功:
NSString *path = [[NSBundle mainBundle] pathForResource:@"record.MP3" ofType:nil];
NSData *fileData = [NSData dataWithContentsOfFile:path];
NSURL * outFileUrl = [NSURL fileURLWithPath:path];
[ZBRequestManager requestWithConfig:^(ZBURLRequest * request) {
request.server= kTestServer;
request.path= kUploadRecordFile;
request.methodType=ZBMethodTypeUpload;
request.requestSerializer = ZBHTTPRequestSerializer;
request.responseSerializer = ZBJSONResponseSerializer;
request.parameters = @{@"submission_id":@"11"};
[request addFormDataWithName:@"file" fileName:@"record.MP3" mimeType:@"audio/mpeg" fileData:fileData];
} progress:^(NSProgress * _Nullable progress) {
NSLog(@"onProgress: %.2f", 100.f * progress.completedUnitCount/progress.totalUnitCount);
} success:^(id responseObject,ZBURLRequest * request) {
NSLog(@"responseObject: %@", responseObject);
} failure:^(NSError * _Nullable error) {
NSLog(@"error: %@", error);
}];
但是会有以下日志:
The request of a upload task should not contain a body or a body stream, use upload(for:fromFile:), upload(for:from:), or supply the body stream through the urlSession(_:needNewBodyStreamForTask:) delegate method.

@Suzhibin
Copy link
Owner

Suzhibin commented Aug 9, 2024

看着像是ContentTypes文件类型的问题 ,我看你上传的是MP3,默认我已经添加了multipart/form-data,应该是可以兼容MP3的,你可以增加一个audio/mpeg 类型试试,是否能消除警告。如果只是警告不影响使用,也可以不用处理
在app 启动时 调用 下面方法,
[ZBRequestManager setupBaseConfig:^(ZBConfig * _Nullable config) {
config.responseContentTypes=@[@"audio/mpeg"];
} @Samuel-Xi

@Samuel-Xi
Copy link
Author

好的👌,谢谢,我试一下

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants