gpt4 book ai didi

ios - 使用 AFNetworking 上传

转载 作者:塔克拉玛干 更新时间:2023-11-02 10:12:45 24 4
gpt4 key购买 nike

NSURL *url = [NSURL URLWithString:@"http://127.0.0.1:8000/photo/"];
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url];
NSDictionary *headers = [NSDictionary dictionaryWithObject:data forKey:@"attachment"];
//NSLog(@"strimng");
NSMutableURLRequest *request = [httpClient multipartFormRequestWithMethod:@"POST" path:@"upload/" parameters:nil constructingBodyWithBlock: ^(id<AFMultipartFormData> formData) {
NSLog(@"strimng");
[formData appendPartWithFileData:data name:@"attachment" 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);
}];
[operation start];

我正在尝试使用 AFNetworking 从 iphone 上传图像,但这不起作用。在注销 formdata appenddata 命令之前的 NSLog。但是后面那个好像没有。我还检查了 NSdata 是否为零,但事实并非如此。而且没有发送请求。请任何人帮助我。

最佳答案

假设您必须上传图像,为什么要使用 data 创建 NSDictionary?这不是必需的,您只需要在 multipartFormRequestWithMethod 中使用 data 即可。

尝试以下操作

// Get the image that you want to upload via ImagePicker or a @property
UIImage *imageIWantToUpload = self.image;

// Create the NSData object for the upload process
NSData *dataToUpload = UIImageJPEGRepresentation(imageIWantToUpload, 0.5);

NSMutableURLRequest *request = [httpClient multipartFormRequestWithMethod:@"POST" path:@"upload/" parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
[formData appendPartWithFileData:dataToUpload name:@"attachment" fileName:@"attachment.jpg" mimeType:@"image/jpg"];
}];

// You can add then the progressBlock and the completionBlock
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[operation setUploadProgressBlock:progress];
[operation setCompletionBlockWithSuccess:success failure:failure];

您还应该仔细检查您的服务器端是否需要一些参数,因为您正在将 NSMutableURLRequest 参数字段设置为 nil

关于ios - 使用 AFNetworking 上传,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13656868/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com