80938

Sending data by FTP

Question:

Im trying to upload a file and im having a few problems. I have an input stream from a local path:

self.stmIn = [NSInputStream inputStreamWithFileAtPath:localPath];

The variable localPath has this URL:

/Users/JBG/Library/Application Support/iPhone Simulator/6.0/Applications/734F4DC6-8683-42BB-AB0D-A5553BC22C55/Documents/100046-003.jpg

I can open the stream without any problem. The problem comes when i try to read it:

bytesRead = [self.stmIn read:self.buffer maxLength:kSendBufferSize];

The result is -1. I dont understand the problem. Any idea?

Thanks and regards

EDIT: the code is based(taken) on the Apple's SimpleFTPSample but i implemented it as a NSObject class beside of UIViewController

EDIT: got this info using the streamError method: Error Domain=NSPOSIXErrorDomain Code=14 "The operation coudn't be completed. Bad address"

EDIT: I add the code:

- (uint8_t *)buffer { return self->buffer; } - (BOOL)isSending { return (self.stmOut != nil); } - (void)startSend { BOOL success; NSURL *url; [[NetworkManager sharedInstance] didStartNetworkOperation]; assert([[NSFileManager defaultManager] fileExistsAtPath:localPath]); assert(self.stmOut == nil); // don't tap send twice in a row! assert(self.stmIn == nil); // ditto url = [[NetworkManager sharedInstance] smartURLForString:ftpPath]; success = (url != nil); if (success) { url = CFBridgingRelease(CFURLCreateCopyAppendingPathComponent(NULL, (CFURLRef) url, (CFStringRef) [localPath lastPathComponent], false)); success = (url != nil); } if ( ! success) { NSLog(@"Invalid URL"); } else { self.stmIn = [NSInputStream inputStreamWithFileAtPath:localPath]; assert(self.stmIn != nil); [self.stmIn open]; self.stmOut = CFBridgingRelease(CFWriteStreamCreateWithFTPURL(NULL, (CFURLRef) url)); assert(self.stmOut != nil); success = [self.stmOut setProperty:ftpUser forKey:(id)kCFStreamPropertyFTPUserName]; assert(success); success = [self.stmOut setProperty:ftpPwd forKey:(id)kCFStreamPropertyFTPPassword]; assert(success); self.stmOut.delegate = self; [self.stmOut scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode]; [self.stmOut open]; } } - (void)stopSendWithStatus:(NSString *)statusString { if (self.stmOut != nil) { [self.stmOut removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode]; self.stmOut.delegate = nil; [self.stmOut close]; self.stmOut = nil; } if (self.stmIn != nil) { [self.stmIn close]; self.stmIn = nil; } if (statusString == nil) { statusString = @"Archivo subido"; } [[NetworkManager sharedInstance] didStopNetworkOperation]; } - (void)stream:(NSStream *)aStream handleEvent:(NSStreamEvent)eventCode { assert(aStream == self.stmOut); switch (eventCode) { case NSStreamEventOpenCompleted: { NSLog(@"Opened connection"); } break; case NSStreamEventHasBytesAvailable: { assert(NO); } break; case NSStreamEventHasSpaceAvailable: { NSLog(@"Sending"); if (self.bufferOffset == self.bufferLimit) { NSInteger bytesRead; bytesRead = [self.stmIn read:self.buffer maxLength:kSendBufferSize]; if (bytesRead == -1) { [self stopSendWithStatus:@"File read error"]; } else if (bytesRead == 0) { [self stopSendWithStatus:nil]; } else { self.bufferOffset = 0; self.bufferLimit = bytesRead; } } if (self.bufferOffset != self.bufferLimit) { NSInteger bytesWritten; bytesWritten = [self.stmOut write:&self.buffer[self.bufferOffset] maxLength:self.bufferLimit - self.bufferOffset]; assert(bytesWritten != 0); if (bytesWritten == -1) { [self stopSendWithStatus:@"Network write error"]; } else { self.bufferOffset += bytesWritten; } } } break; case NSStreamEventErrorOccurred: { [self stopSendWithStatus:@"Stream open error"]; } break; case NSStreamEventEndEncountered: { // ignore } break; default: { assert(NO); } break; } }

Answer1:

Try this code.. Hope it will help you..

- (void)sendDidStart { // self.statusLabel.text = @"Sending"; [[NetworkManager sharedInstance] didStartNetworkOperation]; } - (void)updateStatus:(NSString *)statusString { assert(statusString != nil); //self.statusLabel.text = statusString; } - (void)sendDidStopWithStatus:(NSString *)statusString { if (statusString == nil) { statusString = @"Put succeeded"; } [[NetworkManager sharedInstance] didStopNetworkOperation]; } #pragma mark * Core transfer code // This is the code that actually does the networking. // Because buffer is declared as an array, you have to use a custom getter. // A synthesised getter doesn't compile. - (uint8_t *)buffer { return self->_buffer; } - (BOOL)isSending { return (self.networkStream != nil); } - (void)startSend:(NSString *)filePath { BOOL success; NSURL * url; NSLog(@"localfilepath..:%@",localFilePath); assert(localFilePath != nil); assert([[NSFileManager defaultManager] fileExistsAtPath:localFilePath]); assert( [localFilePath.pathExtension isEqual:@"png"] || [localFilePath.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://yourFTPLink/"]; 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) @"minkle.png" , false) // ); url = CFBridgingRelease( CFURLCreateCopyAppendingPathComponent(NULL, ( CFURLRef) url, ( CFStringRef) imageString , false) ); success = (url != nil); } // If the URL is bogus, let the user know. Otherwise kick off the connection. if ( ! success) { // self.statusLabel.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:localFilePath]; assert(self.fileStream != nil); [self.fileStream open]; // Open a CFFTPStream for the URL. self.networkStream = CFBridgingRelease( CFWriteStreamCreateWithFTPURL(NULL, ( CFURLRef) url) ); assert(self.networkStream != nil); // if ([self.usernameText.text length] != 0) { success = [self.networkStream setProperty:@"yourUserName" forKey:(id)kCFStreamPropertyFTPUserName]; assert(success); success = [self.networkStream setProperty:@"yourPassword" 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]; } } - (void)stopSendWithStatus:(NSString *)statusString { if (self.networkStream != nil) { [self.networkStream removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode]; self.networkStream.delegate = nil; [self.networkStream close]; self.networkStream = nil; } if (self.fileStream != nil) { [self.fileStream close]; self.fileStream = nil; } [self sendDidStopWithStatus:statusString]; } - (void)stream:(NSStream *)aStream handleEvent:(NSStreamEvent)eventCode // An NSStream delegate callback that's called when events happen on our // network stream. { #pragma unused(aStream) assert(aStream == self.networkStream); switch (eventCode) { case NSStreamEventOpenCompleted: { [self updateStatus:@"Opened connection"]; } break; case NSStreamEventHasBytesAvailable: { assert(NO); // should never happen for the output stream } break; case NSStreamEventHasSpaceAvailable: { [self updateStatus:@"Sending"]; // If we don't have any data buffered, go read the next chunk of data. if (self.bufferOffset == self.bufferLimit) { NSInteger bytesRead; bytesRead = [self.fileStream read:self.buffer maxLength:jSendBufferSize]; if (bytesRead == -1) { [self stopSendWithStatus:@"File read error"]; } else if (bytesRead == 0) { [self stopSendWithStatus:nil]; } else { self.bufferOffset = 0; self.bufferLimit = bytesRead; } } // If we're not out of data completely, send the next chunk. if (self.bufferOffset != self.bufferLimit) { NSInteger bytesWritten; bytesWritten = [self.networkStream write:&self.buffer[self.bufferOffset] maxLength:self.bufferLimit - self.bufferOffset]; assert(bytesWritten != 0); if (bytesWritten == -1) { [self stopSendWithStatus:@"Network write error"]; } else { self.bufferOffset += bytesWritten; } } } break; case NSStreamEventErrorOccurred: { [self stopSendWithStatus:@"Stream open error"]; } break; case NSStreamEventEndEncountered: { // ignore } break; default: { assert(NO); } break; } }

Answer2:

Did you turn this string:

/Users/JBG/Library/Application Support/iPhone Simulator/6.0/Applications/734F4DC6-8683-42BB-AB0D-A5553BC22C55/Documents/100046-003.jpg

into a file URL?

You should be using something like fileURLWithPath: from the [NSURL](https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSURL_Class/Reference/Reference.html] docs to turn the string path into a proper URL.

Recommend

  • Unable to see SQLite file in iTunes
  • iOS Memory issue (ARC) when downloading and saving large ammount of images from server
  • How to insert update delete in database operation and also save database in document directory [clos
  • Minecraft One Player Per Config
  • How can I Retrieve a property value from child array object?
  • ansible json filter list
  • How to add database and Nsxmlparser into application?
  • AVAudioPlayer won't play existing file a second time
  • AFNetworking: downloading files in queue
  • Sending data by FTP
  • Syntax error at const char *sql_stmt
  • background device music gets stopped as app starts ios
  • Add icon from array to QListWidget item
  • Leaking NSAutoreleasePool
  • catching unknown host exception in codename one
  • iOS sharing data between viewcontrollers
  • Audio file doesn't play in iPhone 5
  • How do I observe a signal and immediately receive a `next` event if it has already occured?
  • How to implement AVAudioPlayer Inside Singleton Method?
  • Error Domain=NSCocoaErrorDomain Code=3840 “Invalid value around character 0.” UserInfo={NSDebugDescr
  • Is Bolts framework[Parse+Facebook] need to use parse webservice?
  • Google App Indexing not resolving for Swift?
  • simple way to map a json collection response into swift object class
  • Bug in WPF DataGrid
  • Possible to stop flickering java tooltip in heavyweight mode?
  • Can a Chrome extension content script make an jQuery AJAX request for an html file that is itself a
  • Circular dependency while pushing http interceptor
  • Run Powershell script from inside other Powershell script with dynamic redirection to file
  • AngularJs get employee from factory
  • How to set the response of a form post action to a iframe source?
  • Change div Background jquery
  • unknown Exception android
  • EntityFramework adding new object to nested object collection
  • Checking variable from a different class in C#
  • failed to connect to specific WiFi in android programmatically
  • How can I use threading to 'tick' a timer to be accessed by other threads?