Xcode - iOS - 只需将文件上传到 FTP 服务器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17111010/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Xcode - iOS - Simply upload a file to FTP Server
提问by Jonathan Gurebo
I'm trying to work with FTP Servers. I have googled around for everything and everything is hard to understand for beginners like me. SimpleFTPSample is hard to understand because it is so much at a time. views, buttons, labels, textflelds, upload, download, request, list, get. Same with BlackRaccoon and everything else.
我正在尝试使用 FTP 服务器。我已经搜索了所有内容,对于像我这样的初学者来说,一切都很难理解。SimpleFTPSample 很难理解,因为它一次太多。视图、按钮、标签、文本字段、上传、下载、请求、列表、获取。与 BlackRaccoon 和其他所有内容相同。
How to Simply and programily upload "test.txt" to FTP Server: "192.168.1.111" in Xcode (iPhone app) without views or button. Just code that can be in the ViewDidLoad for example.
如何在没有视图或按钮的情况下,在 Xcode(iPhone 应用程序)中简单且程序化地将“test.txt”上传到 FTP 服务器:“192.168.1.111”。例如,可以在 ViewDidLoad 中的代码。
Maybe something like this?:
也许是这样的?:
NSURL* url = [NSURL URLWithString:@"ftp://username:[email protected]"];
CFReadStreamRef stream = CFReadStreamCreateWithFTPURL(NULL, (__bridge CFURLRef) url);
stream.delegate= self;
[stream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[stream open];
but which file? expand this, or write a new code. i don't know, this is new for me.
但哪个文件?展开这个,或者写一个新的代码。我不知道,这对我来说是新的。
Thanks Jonathan
谢谢乔纳森
回答by Lloyd Sargent
As the writer of Black Raccoon perhaps I'm biased (well, I KNOW I'm biased), but I've attempted to make it as simple and powerful as possible. Let's look at what you want to do, upload a file:
作为 Black Raccoon 的作者,我可能有偏见(好吧,我知道我有偏见),但我已经尝试让它尽可能简单和强大。来看看你要做什么,上传一个文件:
There are four things we need to upload a file - start up code, then four delegate methods: overwrite check, data, success and the fail. Let's assume that you read the entire file into memory (okay for small files less than 2 megs).
上传文件需要四件事 - 启动代码,然后是四种委托方法:覆盖检查、数据、成功和失败。假设您将整个文件读入内存(对于小于 2 兆的小文件也可以)。
First, you need this in your header:
首先,您需要在标题中使用它:
BRRequestUpload *uploadData; // Black Raccoon's upload object
NSData *uploadData; // data we plan to upload
Now for the code part:
现在是代码部分:
- (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];
}
The first will be asking your code if you want to overwrite an existing file.
第一个将询问您的代码是否要覆盖现有文件。
-(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;
}
}
Next, Black Raccoon will ask you for chunks of data to send. If you have a very large file you NEVER want to try to send it all in one shot - Apple's API will choke and drop data. However, we only have one small chunk so we do this:
接下来,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;
}
Remember we can ONLYdo this for a small file.
请记住,我们只能对小文件执行此操作。
Next we have our completion handler (if things worked according to plan):
接下来我们有我们的完成处理程序(如果事情按计划进行):
-(void) requestCompleted: (BRRequest *) request
{
if (request == uploadFile)
{
NSLog(@"%@ completed!", request);
uploadFile = nil;
}
}
Lastly we have our failure handler:
最后我们有我们的失败处理程序:
-(void) requestFailed:(BRRequest *) request
{
if (request == uploadFile)
{
NSLog(@"%@", request.error.message);
uploadFile = nil;
}
}
It would be WONDERFUL if it was as simple as saying [BRFtpUploadTo: dest srcfile: srcFile destfile: dstFile]
but there are many reasons why you SHOULDN'T. Part of it has to do with how Apple has implemented their internal FTP. There are also the issues of blocking, errors, etc. In the end, FTP sounds like it should be trivial but ends up being a bit of a nightmare.
如果它像说一样简单,那就太好了,[BRFtpUploadTo: dest srcfile: srcFile destfile: dstFile]
但有很多原因你不应该。部分原因与 Apple 如何实施其内部 FTP 有关。还有阻塞,错误等问题。最终,FTP听起来应该是微不足道的,但最终却是一场噩梦。
FTP is non-trivial which is why there are so many implementations. I'm not arguing that Black Raccoon is the best, but it is maintained with response to issues being between minutes to a couple of days.
FTP 非常重要,这就是为什么有这么多实现。我并不是说 Black Raccoon 是最好的,但它会在几分钟到几天之间对问题的响应进行维护。
It may look daunting at first, but Black Raccoon is, in my opinion, one of the better FTP libraries. I've spent a lot of time and effort to make it a quality product with excellent response to issues. How do I do this for free? Volume. ;)
乍一看可能令人生畏,但在我看来,Black Raccoon 是更好的 FTP 库之一。我花费了大量时间和精力使其成为具有出色问题响应能力的优质产品。我如何免费执行此操作?体积。;)
Good luck with whatever FTP software you end up with!
祝您最终使用的任何 FTP 软件好运!
回答by Lloyd Sargent
Upload path is required when uploading. That is the way FTP works.
The port is the standard FTP port. I know of no way to change this without violating the API. If you figure it out, you stand a good chance of not passing Apple's check.
This code will upload/download any file.
I do not know how to make this work under secure conditions. This uses Apple's FTP protocol. There are other FTP packages that have built this from scratch and are far more intelligent. I would look into them.
上传时需要上传路径。这就是 FTP 的工作方式。
该端口是标准的 FTP 端口。我知道没有办法在不违反 API 的情况下改变它。如果你弄清楚了,你很有可能不会通过苹果的检查。
此代码将上传/下载任何文件。
我不知道如何在安全的条件下进行这项工作。这使用 Apple 的 FTP 协议。还有其他 FTP 包从头开始构建,并且更加智能。我会调查他们。
BR was designed because I needed simple FTP communication. White Raccoon didn't do this for me because at the time (it has since been modernized).
BR 的设计是因为我需要简单的 FTP 通信。White Raccoon 没有为我做这件事,因为当时(它已经现代化了)。