我使用 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];
}
}