加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Xcode – iOS – 只需将文件上传到FTP服务器即可

发布时间:2020-12-14 17:56:55 所属栏目:百科 来源:网络整理
导读:我正在尝试使用FTP服务器. 我已经搜索了所有内容,对于像我这样的初学者来说,一切都很难理解. SimpleFTPSample很难理解,因为它一次很多.视图,按钮,标签,textflelds,上传,下载,请求,列表,获取.与BlackRaccoon和其他一切相同. 如何简单和程序地将“test.txt”上
我正在尝试使用FTP服务器.
我已经搜索了所有内容,对于像我这样的初学者来说,一切都很难理解. SimpleFTPSample很难理解,因为它一次很多.视图,按钮,标签,textflelds,上传,下载,请求,列表,获取.与BlackRaccoon和其他一切相同.

如何简单和程序地将“test.txt”上传到FTP服务器:Xcode(iPhone应用程序)中的“192.168.1.111”没有视图或按钮.只是可以在ViewDidLoad中的代码.

也许这样的事情?:

NSURL* url = [NSURL URLWithString:@"ftp://username:pw@189.92.32.34"];
CFReadStreamRef stream = CFReadStreamCreateWithFTPURL(NULL,(__bridge CFURLRef) url);
stream.delegate= self;
[stream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[stream open];

但是哪个档案?
展开它,或编写新代码.我不知道,这对我来说是新的.

谢谢乔纳森

解决方法

作为Black Raccoon的作者,也许我有偏见(好吧,我知道我有偏见),但我试图让它变得尽可能简单和强大.让我们看看你想做什么,上传一个文件:

我们需要上传文件的四件事 – 启动代码,然后是四个委托方法:覆盖检查,数据,成功和失败.假设您将整个文件读入内存(对于小于2兆的小文件,可以).

首先,在标题中需要这个:

BRRequestUpload *uploadData;  // Black Raccoon's upload object
    NSData *uploadData;           // data we plan to upload

现在代码部分:

- (IBAction) uploadFile :(id)sender
{
    //----- get the file path for the item we want to upload
    NSString *applicationDocumentsDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES) objectAtIndex:0];
    NSString *filepath = [NSString stringWithFormat: @"%@/%@",applicationDocumentsDir,@"file.text"];

    //----- read the entire file into memory (small files only)
    uploadData = [NSData dataWithContentsOfFile: filepath];

    //----- create our upload object
    uploadFile = [[BRRequestUpload alloc] initWithDelegate: self];

    //----- for anonymous login just leave the username and password nil 
    uploadFile.path = @"/home/user/myfile.txt";     
    uploadFile.hostname = @"192.168.1.100";
    uploadFile.username = @"yourusername";
    uploadFile.password = @"yourpassword";

    //----- we start the request
    [uploadFile start];
}

第一个是询问您的代码是否要覆盖现有文件.

-(BOOL) shouldOverwriteFileWithRequest: (BRRequest *) request
{
    //----- set this as appropriate if you want the file to be overwritten
    if (request == uploadFile)
    {
        //----- if uploading a file,we set it to YES (if set to NO,nothing happens)
        return YES;
    }
}

接下来,Black Raccoon将要求您提供要发送的数据块.如果你有一个非常大的文件,你永远不想尝试一次性发送它 – Apple的API将扼杀和丢弃数据.但是,我们只有一个小块,所以我们这样做:

- (NSData *) requestDataToSend: (BRRequestUpload *) request
{
    //----- returns data object or nil when complete
    //----- basically,first time we return the pointer to the NSData.
    //----- and BR will upload the data.
    //----- Second time we return nil which means no more data to send
    NSData *temp = uploadData;   // this is a shallow copy of the pointer

    uploadData = nil;            // next time around,return nil...

    return temp;
}

请记住,我们只能为一个小文件执行此操作.

接下来我们有完成处理程序(如果按照计划工作):

-(void) requestCompleted: (BRRequest *) request
{
    if (request == uploadFile)
    {
        NSLog(@"%@ completed!",request);
        uploadFile = nil;
    }
}

最后我们有失败处理程序:

-(void) requestFailed:(BRRequest *) request
{
    if (request == uploadFile)
    {
        NSLog(@"%@",request.error.message);
        uploadFile = nil;
    }
}

如果它像说[BRFtpUploadTo:dest srcfile:srcFile destfile:dstFile]一样简单,那就太棒了,但是你有多少理由不应该这样做.其中一部分与Apple如何实施其内部FTP有关.还有阻塞,错误等问题.最后,FTP听起来应该是微不足道的,但最终有点像噩梦.

FTP是非常重要的,这就是为什么有这么多的实现.我不是在争论Black Raccoon是最好的,但是它会在几分钟到几天之间的问题上得到回应.

一开始可能看起来令人生畏,但在我看来,Black Raccoon是更好的FTP库之一.我花了很多时间和精力使它成为一个高质量的产品,对问题的反应非常好.我该如何免费这样做?体积.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读