ファイルを ftp サーバーにアップロードするためのガイドとして、iOS の simpleFTP の例を使用しています ( 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];
}
}