如何在ios中的ftp服务器上上载.png或.jpg图像。?
我想从iOS应用程序上传或保存图像到FTP服务器。但是每次我收到错误,ftp未连接 我使用SCRFTPRequest库 这是我的密码如何在ios中的ftp服务器上上载.png或.jpg图像。?,ios,iphone,ftp,Ios,Iphone,Ftp,我想从iOS应用程序上传或保存图像到FTP服务器。但是每次我收到错误,ftp未连接 我使用SCRFTPRequest库 这是我的密码 UIImage *image = [info objectForKey:@"UIImagePickerControllerOriginalImage"]; NSData * imageData = UIImagePNGRepresentation(image); NSFileManager * fileManager = [NSFileManager defaul
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];
从
只需拖放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;
}
最后,我成功地将图像文件上传到ftp服务器上 为了在ftp上上传图像,我使用了金浣熊外部库。有了这个库,您可以轻松地将图像上传到ftp服务器
你可以使用它。@HimanshuJoshi在你的链接中有一个php链接需要指定,但我不知道,请帮助我解决。尝试使用白浣熊我不知道
CFUrl
你能给我完整的代码plz吗,因为我是ios新手。它抛出未知错误。。和委托方法未调用。是否已在文件WebRequestDelegate
中添加协议。查看我的完整答案是的,我添加了它,但它抛出[WRRequestListDirectory stream:handleEvent:[Line 968]未知错误!以及如何写url路径和我的地址,现在我想把图片保存在这里。怎么写。什么是主机名,什么是路径。/很好的链接。这真的帮助我摆脱了在FTP上保存图像的大问题。thanx.@RamaniAshish支持后台上传,这意味着整个应用程序进入后台模式,正常情况下,除了NSURLSERSESSION之外,所有执行都将停止,这有效吗?