gpt4 book ai didi

ios - 如何在发布数据 iOS objective-c 中将图像附件发送到服务器

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:53:36 25 4
gpt4 key购买 nike

向服务器发送数据在 postman 中有效,但在 Objective-C 中出现错误。

image1

我试图从服务器上实现这个失败。我在下面提到的链接不起作用。获取错误上传失败。我做错了什么?

Upload image to server ios

how to POST value while uploading image in iOS objective c

我的代码:

NSData *dataImage = UIImageJPEGRepresentation(myImage, 1.0f);

// set your URL Where to Upload Image
NSString *urlString = @"http://xxxxxxxxxxxxxxxx/index.php/API/uploadClaim";

NSDictionary *user = [[NSUserDefaults standardUserDefaults] objectForKey:@"userDetails"];

// Create 'POST' MutableRequest with Data and Other Image Attachment.

NSMutableDictionary* _params = [[NSMutableDictionary alloc] init];
[_params setObject:@"filename" forKey:@"file_name"];
[_params setObject:@"photo" forKey:@"file_type"];
[_params setObject:[[user objectForKey:@"user_data"] objectForKey:@"id"] forKey:@"user_id"];

NSString *BoundaryConstant = @"----------V2ymHFg03ehbqgZCaKO6jy";
NSString* FileParamConstant = @"image";

NSURL* requestURL = [NSURL URLWithString:urlString];

// create request
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
[request setHTTPShouldHandleCookies:NO];
[request setTimeoutInterval:120];
[request setHTTPMethod:@"POST"];

NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", BoundaryConstant];
[request setValue:contentType forHTTPHeaderField: @"Content-Type"];

// post body
NSMutableData *body = [NSMutableData data];

// add params (all params are strings)
for (NSString *param in _params)
{
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", BoundaryConstant] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n", param] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"%@\r\n", [_params objectForKey:param]] dataUsingEncoding:NSUTF8StringEncoding]];
}

// add image data
if (dataImage) {
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", BoundaryConstant] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"image.jpg\"\r\n", FileParamConstant] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Type: image/jpeg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:dataImage];
[body appendData:[[NSString stringWithFormat:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
}

[body appendData:[[NSString stringWithFormat:@"--%@--\r\n", BoundaryConstant] dataUsingEncoding:NSUTF8StringEncoding]];

// setting the body of the post to the reqeust
[request setHTTPBody:body];

// set the content-length
NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[body length]];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
// set URL
[request setURL:requestURL];

NSURLResponse *response = nil;
NSError *requestError = nil;

NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&requestError];
NSDictionary* json = [NSJSONSerialization JSONObjectWithData:returnData options:kNilOptions error:&requestError];

最佳答案

这是我上传一个或多个图像文件的工作代码:

请注意:您应该使用正确的键值在 NSDictionary 中传递图像/文件数据,并且您应该将键名设置为服务器在上传调用中期望的名称。
例如在你的情况下它是 file

parameters 字典将是您要与 file

一起发送的其他参数
- (void) performNetworkEventRequestCallWithFileUpload :(NSMutableDictionary*) imagesData : (NSMutableDictionary*)parameters completionBlock:(void (^)(BOOL succeeded, NSDictionary *dict))completionBlock {


NSMutableURLRequest *urlRequest = [[NSMutableURLRequest alloc] init];
[urlRequest setURL:[NSURL URLWithString:BASE_URL]];
[urlRequest setHTTPMethod:@"POST"];
NSString *boundary = @"---------------------------14737809831466499882746641449";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
[urlRequest addValue:contentType forHTTPHeaderField: @"Content-Type"];
NSMutableData *body = [NSMutableData data];


[parameters enumerateKeysAndObjectsUsingBlock: ^(NSString *key, NSString *object, BOOL *stop) {
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n",key] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"%@",object] dataUsingEncoding:NSUTF8StringEncoding]];
}];


[imagesData enumerateKeysAndObjectsUsingBlock: ^(NSString *key, NSData *object, BOOL *stop) {
if ([object isKindOfClass:[NSData class]]) {
if (object.length > 0) {
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
NSString *timeSTamp = [NSString stringWithFormat:@"%@",[NSDate date]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"%@.jpg\"\r\n",key,timeSTamp] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:object]];
}

}
}];


[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[urlRequest setHTTPBody:body];


AFHTTPSessionManager* manager = [AFHTTPSessionManager manager];
manager.responseSerializer.acceptableContentTypes = nil;

NSURLSessionDataTask *dataTask = [manager dataTaskWithRequest:urlRequest completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
if (error) {
NSLog(@"Error: %@", error);
completionBlock (NO, responseObject);
} else {
NSLog(@"success");
completionBlock (YES, responseObject);
}
}];
[dataTask resume];
}

关于ios - 如何在发布数据 iOS objective-c 中将图像附件发送到服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47874414/

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