美文网首页iOS框架
Wi-Fi传输文件

Wi-Fi传输文件

作者: 幻想无极 | 来源:发表于2017-12-19 11:04 被阅读55次

    SGWiFiUpload

    • SGWiFiUpload使用了CocoaHttpServer创建服务器,接受并监听文件
    • 使用SGHTTPConnection继承HTTPConnection修改了里面的文件储存路径,并且使用通知监听了里面的上传进度

    SGHTTPConnection类

    - (void) processStartOfPartWithHeader:(MultipartMessageHeader*) header 
    
        // 这里用于发出文件开始上传的通知
        dispatch_async(dispatch_get_main_queue(), ^{
            [[NSNotificationCenter defaultCenter] postNotificationName:SGFileUploadDidStartNotification object:@{@"fileName" : filename ?: @"File"}];
        });
        // 这里用于设置文件的保存路径,先预存一个空文件,然后进行追加写内容
        NSString *uploadDirPath = [SGWiFiUploadManager sharedManager].savePath;
    

    SGWiFiUploadManager类

    • 控制服务器开关
    • 接受文件上传进度并回调出去
    • 弹出上传界面

    例子

       SGWiFiUploadManager *mgr = [SGWiFiUploadManager sharedManager];
        BOOL success = [mgr startHTTPServerAtPort:10086];
        if (success) {
            [mgr setFileUploadStartCallback:^(NSString *fileName, NSString *savePath) {
                NSLog(@"File %@ Upload Start", fileName);
            }];
            [mgr setFileUploadProgressCallback:^(NSString *fileName, NSString *savePath, CGFloat progress) {
                NSLog(@"File %@ on progress %f", fileName, progress);
            }];
            [mgr setFileUploadFinishCallback:^(NSString *fileName, NSString *savePath) {
                NSLog(@"File Upload Finish %@ at %@", fileName, savePath);
            }];
        }
        [mgr showWiFiPageFrontViewController:self dismiss:^{
            [mgr stopHTTPServer];
        }];
    

    相关文章

      网友评论

        本文标题:Wi-Fi传输文件

        本文链接:https://www.haomeiwen.com/subject/wlrbwxtx.html