gpt4 book ai didi

ios - 用于后台文件传输的 GCDWebServer 处理程序(不是 GCDWebUploader)

转载 作者:行者123 更新时间:2023-11-28 19:53:54 32 4
gpt4 key购买 nike

有没有人已经尝试在不使用包含的(很酷的)GCDWebUploader 的情况下实现 GET 请求的处理程序(POST 方法的相同问题)?

我需要服务器响应 GET 请求 http://local/download/filename.ext 将文件上传到客户端。

我正在使请求符合代码“BackgroundSessionManager”(可在此处获得:AFNetworking 2.0 and background transfers),它已发送并触发,不用担心。

我在服务器端获取的日志是这样写的:

[DEBUG] Did start background task
[DEBUG] Connection received 248 bytes on socket 14
[DEBUG] Connection on socket 14 preflighting request "GET /download/file.ext with 248 bytes body
[DEBUG] Connection on socket 14 processing request "GET /download/file.ext" with 248 bytes body
[EXCEPTION] *** +[NSJSONSerialization dataWithJSONObject:options:error:]: value parameter is nil
[DEBUG] Did close connection on socket 14

我不知道如何设置处理程序,以便不关心从 JSON 解析的不存在的查询。

[webServer addHandlerForMethod:@"GET" path:@"/download" requestClass:[GCDWebServerRequest class] processBlock:^GCDWebServerResponse *(GCDWebServerRequest* request) {
__strong AppDelegate* strongSelf = weakSelf;
NSLog(@"request for download is %@", request);

return [strongSelf downloadFile:request];
}];

最佳答案

为了让它工作,我删除了这段代码:

/*
// Add a handler to respond to GET requests
[webServer addDefaultHandlerForMethod:@"GET"
requestClass:[GCDWebServerRequest class]
asyncProcessBlock:^(GCDWebServerRequest* request, GCDWebServerCompletionBlock completionBlock) {

__strong AppDelegate* strongSelf = weakSelf;
.....
*/

[webServer addHandlerForMethod:@"GET" path:@"/download" requestClass:[GCDWebServerRequest class] processBlock:^GCDWebServerResponse *(GCDWebServerRequest* request) {
__strong AppDelegate* strongSelf = weakSelf;
NSLog(@"request for download is %@", request);

return [strongSelf downloadFile:request];
}];

注释掉正在接管并无论如何都期待 JSON 数据包的默认处理程序。

更新

要使用 GCDWebServer 和 NSURLSessions(即使使用 AFNetworking)实现后台文件传输,对我来说最好的方法是使用 MatchBlock 实例化一个 GET 处理程序,如下所示:

[webServer addHandlerWithMatchBlock:^GCDWebServerRequest *(NSString* requestMethod, NSURL* requestURL, NSDictionary* requestHeaders, NSString* urlPath, NSDictionary* urlQuery) {

if (![requestMethod isEqualToString:@"GET"]) {
return nil;
}
if (![urlPath hasPrefix:@"/download"]) {
return nil;
}
return [[GCDWebServerRequest alloc] initWithMethod:requestMethod url:requestURL headers:requestHeaders path:urlPath query:urlQuery];

} processBlock:^GCDWebServerResponse *(GCDWebServerRequest* request) {

GCDWebServerResponse* response = nil;

NSString* filePath = [[weakSelf applicationDocumentsDirectory] stringByAppendingPathComponent:[[request.path stringByRemovingPercentEncoding]];
NSString* fileType = [[[NSFileManager defaultManager] attributesOfItemAtPath:filePath error:NULL] fileType];
if (fileType) {
if ([fileType isEqualToString:NSFileTypeRegular]) {
// always allow ranges in our requests
response = [GCDWebServerFileResponse responseWithFile:filePath byteRange:request.byteRange];
[response setValue:@"bytes" forAdditionalHeader:@"Accept-Ranges"];
}
}
if (response) {
response.cacheControlMaxAge = 360;
} else {
response = [GCDWebServerResponse responseWithStatusCode:kGCDWebServerHTTPStatusCode_NotFound];
}
return response;

}];

关于ios - 用于后台文件传输的 GCDWebServer 处理程序(不是 GCDWebUploader),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27650949/

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