代码之家  ›  专栏  ›  技术社区  ›  Ashish Ramani Sreejith Bhatt

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

  •  3
  • Ashish Ramani Sreejith Bhatt  · 技术社区  · 10 年前

    我想从我的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 回复  |  直到 10 年前
        1
  •  0
  •   Himanshu Joshi    10 年前

    从…起 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;
    
        }
    
        2
  •  0
  •   Ashish Ramani Sreejith Bhatt    10 年前

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

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

    https://github.com/albertodebortoli/GoldRaccoon