【问题标题】:iOS objective c uploading csv file to ftp serveriOS目标c将csv文件上传到ftp服务器
【发布时间】:2013-01-11 00:12:06
【问题描述】:

我使用 iOS simpleFTP 示例作为将文件上传到我的 ftp 服务器 (http://developer.apple.com/library/ios/#samplecode/SimpleFTPSample/Listings/Read_Me_About_SimpleFTPSample_txt.html) 的指南。该文件在本地包含数据,但在我的应用程序将文件上传到我的 ftp 服务器后,它显示为空(0 字节)。谁能在我的代码中看到为什么会这样?代码如下:

    NSString *resultLine = [NSString stringWithFormat:@"%@,%@,%@",
                            aField.text, bField.text, cField.text];

    NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];

    NSString *csvFileName = [NSString stringWithFormat:@"%@.csv",aField.text];

    NSString *reportsCSV = [docPath stringByAppendingPathComponent:csvFileName];

    if([[NSFileManager defaultManager] fileExistsAtPath:reportsCSV])
    {
        [[NSFileManager defaultManager] removeItemAtPath:reportsCSV error:NULL];
    } else{
        [[NSFileManager defaultManager] createFileAtPath:reportsCSV contents:nil attributes:nil];
    }

    NSFileHandle *fileHandle = [NSFileHandle fileHandleForUpdatingAtPath:reportsCSV];
    [fileHandle seekToEndOfFile];
    [fileHandle writeData:[resultLine dataUsingEncoding:NSUTF8StringEncoding]];
    [fileHandle closeFile];

    if([[NSFileManager defaultManager] fileExistsAtPath:reportsCSV])
    {
        NSLog(@"File report type CSV: %@", reportsCSV);
        if ( ! self.isSending ) {
            [self startSend:reportsCSV];
        }

    }

startSend 方法:

    - (void)startSend:(NSString *)filePath
{
BOOL                    success;
NSURL *                 url;

assert(filePath != nil);
//assert([[NSFileManager defaultManager] fileExistsAtPath:filePath]);
//assert( [filePath.pathExtension isEqual:@"png"] || [filePath.pathExtension isEqual:@"jpg"] );

assert(self.networkStream == nil);      // don't tap send twice in a row!
assert(self.fileStream == nil);         // ditto

// First get and check the URL.

url = [[NetworkManager sharedInstance] smartURLForString:@"ftp.fooftp.com"];
success = (url != nil);

if (success) {
    // Add the last part of the file name to the end of the URL to form the final 
    // URL that we're going to put to.

    url = CFBridgingRelease(
                            CFURLCreateCopyAppendingPathComponent(NULL, (__bridge CFURLRef) url, (__bridge CFStringRef) [filePath lastPathComponent], false)
                            );
    success = (url != nil);
}

// If the URL is bogus, let the user know.  Otherwise kick off the connection.

if ( ! success) {
    self.sendingLabel.text = @"Invalid URL";
} else {

    // Open a stream for the file we're going to send.  We do not open this stream; 
    // NSURLConnection will do it for us.

    self.fileStream = [NSInputStream inputStreamWithFileAtPath:filePath];
    assert(self.fileStream != nil);

    [self.fileStream open];

    // Open a CFFTPStream for the URL.

    self.networkStream = CFBridgingRelease(
                                           CFWriteStreamCreateWithFTPURL(NULL, (__bridge CFURLRef) url)
                                           );
    assert(self.networkStream != nil);


        success = [self.networkStream setProperty:@"user" forKey:(id)kCFStreamPropertyFTPUserName];
        assert(success);
        success = [self.networkStream setProperty:@"pass" forKey:(id)kCFStreamPropertyFTPPassword];
        assert(success);

    self.networkStream.delegate = self;
    [self.networkStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
    [self.networkStream open];

    // Tell the UI we're sending.

    [self sendDidStart];
    }
}

【问题讨论】:

  • 我强烈建议使用现有框架来处理互联网请求:RestKit、AFNetworking 等
  • 你也可以使用 NSUrlConnection - 它也知道 FTP。都是非常高的水平。

标签: objective-c ios ios5


【解决方案1】:

我也有同样的情况,但是有 txt 文件和图像文件... 我有一个上传文件的类,这个类基于视图控制器示例,并且在同一个项目中也有来自示例的视图控制器,上传文件的类是空的(0字节),但这不会发生视图控制器,这会正确上传文件...

【讨论】:

    猜你喜欢
    • 2013-06-09
    • 1970-01-01
    • 2011-02-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多