OStack程序员社区-中国程序员成长平台

标题: ios - 如何使用字典 iOS 在服务器上发布图像? [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 15:58
标题: ios - 如何使用字典 iOS 在服务器上发布图像?

我有一个包含一些键/值的 dictionary (dict) 和一个 image。现在我想用字典将图像上传到服务器。我已经尝试过了,但没有取得任何成功。这是我的示例代码--

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];

[request setURL:[NSURL URLWithString:urlString]];

[request setHTTPMethod"OST"];

NSMutableData *body = [NSMutableData data];

NSString *boundary = @"---------------------------14737809831466499882746641449";

NSString *contentType = [NSString stringWithFormat"multipart/form-data; boundary=%@",boundary];

[request addValue:contentType forHTTPHeaderField: @"Content-Type"];

NSData *imageData = UIImageJPEGRepresentation(image,0.5);//or you can use png representation- UIImagePNGRepresentation(image);

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

[body appendData:[@"Content-Disposition: form-data; name=\"thumbnail\"; filename=\"image.png\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];

[body appendData:[@"Content-Type: image/png\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];

[body appendData:imageData];

[body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];

//  parameter all_data
[body appendData:[[NSString stringWithFormat"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];

[body appendData:[[NSString stringWithFormat"Content-Disposition: form-data; name=\"all_data\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];

[body appendData:[[NSString stringWithFormat"%@",dict] dataUsingEncoding:NSUTF8StringEncoding]];

[body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];

// close form
[body appendData:[[NSString stringWithFormat"--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];

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

NSError *error=nil;
NSHTTPURLResponse *response=nil;
NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

我需要帮助我哪里出错了..提前谢谢



Best Answer-推荐答案


试试下面的代码:

    -(void)uploadImageNSString *)api NSDictionary *)params : (UIImage *)image {

    NSURL *myURL = [NSURL URLWithString:api];
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:myURL];
    [request setHTTPMethod"OST"];
    [request setValue"application/json" forHTTPHeaderField"Accept"];
    NSString *boundary = @"---------------------------14737809831466499882746641449";
    NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@; charset=UTF-8", boundary];
    [request addValue:contentType forHTTPHeaderField:@"Content-Type"];
    NSMutableData *theBodyData = [NSMutableData data];
    for (int i=0; i<[[params allKeys] count]; i++)
    {
        [theBodyData appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
        NSString *value=@"";
        value=[params objectForKey:[[params allKeys] objectAtIndex:i]];
        NSString * value1=[NSString stringWithFormat:@"Content-Disposition: form-data; name=%@\r\n\r\n%@\r\n",[[params allKeys] objectAtIndex:i],value];
        [theBodyData appendData:[value1 dataUsingEncoding:NSUTF8StringEncoding]];
    }

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

    [theBodyData appendData:[@"Content-Disposition: form-data; name=\"my_file1\"; filename=\"image1.jpeg\"\r\n" dataUsingEncoding:NSASCIIStringEncoding]];

    [theBodyData appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSASCIIStringEncoding]];
    NSData *myData = UIImageJPEGRepresentation(image,0.5);
    [theBodyData appendData:[NSData dataWithData:myData]];

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



    [request setHTTPBody:theBodyData];
    [request setValue:contentType forHTTPHeaderField: @"Content-Type"];
    [request setValue:[NSString stringWithFormat:@"%d", (int)[theBodyData length]] forHTTPHeaderField:@"Content-Length"];
    [request setHTTPBody: theBodyData];

    NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
    NSURLSession *session = [NSURLSession sessionWithConfiguration:config];

    NSURLSessionDataTask * dataTask =[session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
        if(error == nil) {
            NSString *responseString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
            NSLog(@"Server Raw Response : %@",responseString);
        }

    }];

    [dataTask resume];


}

关于ios - 如何使用字典 iOS 在服务器上发布图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36745032/






欢迎光临 OStack程序员社区-中国程序员成长平台 (http://ostack.cn/) Powered by Discuz! X3.4