2014-03-31 59 views
3

我想從我的iOS應用上傳或保存圖像到FTP服務器。但每次我得到錯誤,ftp未連接如何在ios上的ftp服務器上傳圖片.png或.jpg?

我使用SCRFTPRequest庫。

這裏是我的代碼...

UIImage *image = [info objectForKey:@"UIImagePickerControllerOriginalImage"]; 
NSData * imageData = UIImagePNGRepresentation(image); 
NSFileManager * fileManager = [NSFileManager defaultManager]; 
NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString * documentsDirectory = [paths objectAtIndex:0]; 
NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.png",image]]; 
[fileManager createFileAtPath:fullPath contents:imageData attributes:nil]; 
NSLog(@"image saved"); 
[picker dismissViewControllerAnimated:YES completion:nil]; 

ftpRequest = [SCRFTPRequest requestWithURL:[NSURL URLWithString:@"ftp://myURL"] toUploadFile:fullPath]; 
ftpRequest.username = @"DemoUser"; 
ftpRequest.password = @"DemoUser"; 
ftpRequest.customUploadFileName = @"inapp"; 
ftpRequest.delegate = self; 
[ftpRequest startAsynchronous]; 
+0

您可以使用[QueueFTP(https://github.com/soulslicer/QueueFTP)了點。 –

+0

@HimanshuJoshi在你的鏈接有一個PHP鏈接是指定的,但我不知道它可以幫助我拯救出來。 –

+0

嘗試使用白浣熊https://github.com/valentinradu/WhiteRaccoon –

回答

1

最後我成功上傳了ftp服務器上的圖片文件。

要在ftp上傳圖像,我使用了Gold Raccoon外部庫。藉助此庫,您可以輕鬆地將圖像上傳到ftp服務器。

https://github.com/albertodebortoli/GoldRaccoon

+0

即是。這真的幫我擺脫了在FTP上保存圖像的大問題。感謝名單。 –

+0

@RamaniAshish是否支持後臺上傳,也就是說讓整個應用程序進入後臺模式,除NSUrlSession外,通常所有的執行都會停止,這是否行得通? –

0

White Raccoon

只需拖放WhiteRaccoon.hWhiteRaccoon.m文件,並在你的項目導入CFNetwork框架。

- (void) upload 
    { 

     //the upload request needs the input data to be NSData 
     //so we first convert the image to NSData 
     UIImage * ourImage = [UIImage imageNamed:@"space.jpg"]; 
     NSData * ourImageData = UIImageJPEGRepresentation(ourImage, 100); 


     //we create the upload request 
     //we don't autorelease the object so that it will be around when the callback gets called 
     //this is not a good practice, in real life development you should use a retain property to store a reference to the request 
     WRRequestUpload * uploadImage = [[WRRequestUpload alloc] init]; 
     uploadImage.delegate = self; 

     //for anonymous login just leave the username and password nil 
     uploadImage.hostname = @"xxx.xxx.xxx.xxx"; 
     uploadImage.username = @"myuser"; 
     uploadImage.password = @"mypass"; 

     //we set our data 
     uploadImage.sentData = ourImageData; 

     //the path needs to be absolute to the FTP root folder. 
     //full URL would be ftp://xxx.xxx.xxx.xxx/space.jpg 
     uploadImage.path = @"/space.jpg"; 

     //we start the request 
     [uploadImage start]; 

    } 

    -(void) requestCompleted:(WRRequest *) request{ 

     //called if 'request' is completed successfully 
     NSLog(@"%@ completed!", request); 

    } 

    -(void) requestFailed:(WRRequest *) request{ 

     //called after 'request' ends in error 
     //we can print the error message 
     NSLog(@"%@", request.error.message); 

    } 

    -(BOOL) shouldOverwriteFileWithRequest:(WRRequest *)request { 

     //if the file (ftp://xxx.xxx.xxx.xxx/space.jpg) is already on the FTP server,the delegate is asked if the file should be overwritten 
     //'request' is the request that intended to create the file 
     return YES; 

    } 
+0

你可以給我完整的代碼PLZ,因爲我是新來的IOS。 –

+0

請參閱我編輯的代碼 –

+0

它會拋出未知錯誤..並委託方法未調用。 –