ios - 用nsurlsessionuploadtask上传图片失败,怎么破?

 mobiledu2502878157 发布于 2022-11-02 11:46

代码如下:

    NSString *uploadPicURLStr = @"/jf/platform/upload/appUpload";
    NSString *urlStr = [NSString stringWithFormat:@"%@%@",BASEURL,uploadPicURLStr];
    NSLog(@"urlstr:%@",urlStr);
    NSURL *url = [NSURL URLWithString:urlStr];
//    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    [request addValue:@"multipart/form-data" forHTTPHeaderField:@"Content-type"];

    [request addValue:@"text/html" forHTTPHeaderField:@"Accept"];
    [request setHTTPMethod:@"POST"];
    [request setCachePolicy:NSURLRequestReloadIgnoringCacheData];
    [request setTimeoutInterval:30];
    NSLog(@"request:%@",request);

    NSURLSessionConfiguration *sessionConfig = [NSURLSessionConfiguration defaultSessionConfiguration];
    NSURLSession *session = [NSURLSession sessionWithConfiguration:sessionConfig delegate:self delegateQueue:nil];
//    NSURLSession *session = [NSURLSession sharedSession];
    NSURLSessionUploadTask *uploadTask   = [session uploadTaskWithRequest:request fromData:imgData completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
//        NSLog(@"data:%@",data);
        NSString *string = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
        NSLog(@"data str:%@",string);
        NSLog(@"success:%@",response);
        NSLog(@"error:%@",error);
    }];
    [uploadTask resume];

imgdata是传过来的nsdata图片数据,
报了个:Separation boundary was not specified 错。
请问怎么破?
更新:打印了上传的数据,

-(void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didSendBodyData:(int64_t)bytesSent totalBytesSent:(int64_t)totalBytesSent totalBytesExpectedToSend:(int64_t)totalBytesExpectedToSend{
    NSLog(@"already sent:%lld",bytesSent);
    NSLog(@"totoal to send:%lld",totalBytesSent);
    NSLog(@"expected send:%lld",totalBytesExpectedToSend);
}

输出结果如下:
2015-06-09 10:42:29.013 Purchaser[1492:52094] already sent:32768
2015-06-09 10:42:29.013 Purchaser[1492:52094] totoal to send:32768
2015-06-09 10:42:29.013 Purchaser[1492:52094] expected send:113761
2015-06-09 10:42:29.013 Purchaser[1492:52097] already sent:32768
2015-06-09 10:42:29.014 Purchaser[1492:52097] totoal to send:65536
2015-06-09 10:42:29.014 Purchaser[1492:52097] expected send:113761
2015-06-09 10:42:29.014 Purchaser[1492:52094] already sent:32768
2015-06-09 10:42:29.014 Purchaser[1492:52094] totoal to send:98304
2015-06-09 10:42:29.015 Purchaser[1492:52094] expected send:113761
2015-06-09 10:42:29.015 Purchaser[1492:52097] already sent:15457
2015-06-09 10:42:29.015 Purchaser[1492:52097] totoal to send:113761
2015-06-09 10:42:29.015 Purchaser[1492:52097] expected send:113761

看起来是传出去了,但是服务器那边没收到,头疼。。。。

1 个回答
  • [request setValue:[NSString stringWithFormat:@"multipart/form-data; boundary=%@", _boundary] forHTTPHeaderField:@"Content-Type"];
    上传要指定boundary,没有boundary服务器是无法解析body的数据的

    2022-11-04 11:06 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有