【问题标题】:How to upload image either .png or .jpg on ftp server in ios.?如何在 ios 的 ftp 服务器上上传 .png 或 .jpg 的图像?
【发布时间】:2014-03-31 06:00:56
【问题描述】:

我想从我的 iOS 应用程序上传或保存图像到 FTP 服务器。但每次我收到 ftp not connected

的错误

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

【问题讨论】:

标签: ios iphone ftp


【解决方案1】:

来自White Raccoon,

只需拖放WhiteRaccoon.h 和WhiteRaccoon.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;

    }

【讨论】:

  • 你能给我完整的代码吗,因为我是 ios 新手。
  • 它抛出未知错误..并且未调用委托方法。
  • 您是否在文件WebRequestDelegate 中添加了协议。查看我的完整答案
  • 是的,我添加了它,但它抛出 [WRRequestListDirectory stream:handleEvent:] [Line 968] 未知错误!
  • 以及如何编写 url 路径以及在哪里假设我的 example.com/demo1 现在我想在这里保存图像。怎么写。什么是主机名,什么是路径。/
【解决方案2】:

终于在ftp服务器上上传图片文件成功了。

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

https://github.com/albertodebortoli/GoldRaccoon

【讨论】:

  • 很好的链接。这真的帮助我摆脱了在 FTP 上保存图像的大问题。谢谢。
  • @RamaniAshish 是否支持后台上传,这意味着可以说整个应用程序进入后台模式,通常除了 NSUrlSession 之外所有的执行都会停止,这行得通吗?
猜你喜欢
  • 1970-01-01
  • 2015-06-21
  • 2015-06-16
  • 1970-01-01
  • 2012-01-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-05
相关资源
最近更新 更多