• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - 如何使用字典 iOS 在服务器上发布图像?

[复制链接]
菜鸟教程小白 发表于 2022-12-13 15:58:51 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我有一个包含一些键/值的 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/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap