0

我使用以下代码实现从 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 来上传文件?

你能帮助我吗?谢谢

4

2 回答 2

0

从这里下载文件。 这是在 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];

}

希望这会帮助你。

于 2013-06-24T04:50:20.783 回答
0

您必须使用 FTP 而不是 HTTP。

您可以使用 FTP 将图像上传/下载到服务器,而不是使用 HTTP。

试试这个FTPHelper

于 2013-06-24T04:45:40.463 回答