我使用以下代码从UIImagePickerController(使用AFNETWorking库)中选择上传图像到服务器:
NSURL *url = [NSURL URLWithString:@"http://url"];
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url];
UIImage *imageIWantToUpload = imageupload;
NSData *dataToUpload = UIImageJPEGRepresentation(imageIWantToUpload, 0.5);
// Create the NSData object for the upload process
NSMutableURLRequest *request = [httpClient multipartFormRequestWithMethod:@"POST" path:@"iosuploads.php" parameters:nil constructingBodyWithBlock: ^(id<AFMultipartFormData> formData) {
NSLog(@"strimng");
[formData appendPartWithFileData:dataToUpload name:@"uploadedfile" fileName:@"attachment.jpg" mimeType:@"image/jpeg"];
NSLog(@"strimng");
}];
NSLog(@"strimng");
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[operation setUploadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {
NSLog(@"Sent %lld of %lld bytes", totalBytesWritten, totalBytesExpectedToWrite);
}];
[httpClient enqueueHTTPRequestOperation:operation];
当我在iphone上构建并运行它时,在控制台显示图像上传的大小但是在服务器中找不到刚刚上传的图像。
我在000webhost中创建帐户进行测试,但我在其中使用帐户FTP进行测试。我不知道我必须使用什么URL上传文件?
你能帮帮我吗?感谢答案 0 :(得分:0)
答案 1 :(得分:0)
从here.下载文件 这是在ftp服务器上上传图像的代码。
NSString *ftpUrl=@"ftp://yourftp Url";
NSString *strPortNumber=@"port Number";
NSString *strurlUpload=[NSString stringWithFormat:@"%@:%@",ftpUrl,strPortNumber];
NSString *getfilePath=@"Your Image file path";
SCRFTPRequest *ftpRequest = [[SCRFTPRequest alloc] initWithURL:[NSURL URLWithString:strurlUpload] toUploadFile:getfilePath];
ftpRequest.username = @"Username";
ftpRequest.password = @"password";
ftpRequest.delegate = self;
ftpRequest.didFinishSelector = @selector(uploadFinished:);
ftpRequest.didFailSelector = @selector(uploadFailed:);
ftpRequest.willStartSelector = @selector(uploadWillStart:);
ftpRequest.didChangeStatusSelector = @selector(requestStatusChanged:);
ftpRequest.bytesWrittenSelector = @selector(uploadBytesWritten:);
[ftpRequest startRequest];
从UIImagepickerController获取文件路径。
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo{
NSData *imageData = UIImagePNGRepresentation(image);
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
// Here is the file path for the image
NSString *filepath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"imagename.png"]];
[imageData writeToFile:filepath options:NSDataWritingAtomic error:nil];
[self dismissModalViewControllerAnimated:YES];
}
希望这会对你有所帮助。