如何在ios的ftp服务器上上传.png或.jpg图像?

时间:2014-03-31 06:00:56

标签: ios iphone ftp

我想从我的iOS应用程序上传或保存图像到FTP服务器。但每当我收到 ftp未连接

的错误时

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

2 个答案:

答案 0 :(得分:1)

最后,我在ftp服务器上上传图像文件取得了成功。

要在ftp上传图像,我使用了Gold Raccoon外部库。使用此库,您可以轻松地将图像上传到ftp服务器。

https://github.com/albertodebortoli/GoldRaccoon

答案 1 :(得分: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;

    }