AFNetworking 3 AFMultipartFormData获取字节数据作为响应 [英] AFNetworking 3 AFMultipartFormData getting bytes data in response

查看:636
本文介绍了AFNetworking 3 AFMultipartFormData获取字节数据作为响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在这样做是将数组中的图像发送到服务器:

I'm doing this to send images in an array to server:

NSString *string = [NSString stringWithFormat:@"%@/API/Upload",BaseURLString];
    AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc]initWithBaseURL:[NSURL URLWithString:BaseURLString]];

    [manager setRequestSerializer:[AFHTTPRequestSerializer serializer]];
    [manager setResponseSerializer:[AFHTTPResponseSerializer serializer]];

    for(NSData *eachImage in self.fetchedAtt) {
    [manager POST:string
       parameters:nil
constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {

    [formData appendPartWithFormData:eachImage
                                name:@"image1"];

}

         progress:nil
          success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {

              if (responseObject != nil)
              {
                  //NSDictionary *jsonDictionary = responseObject;
                  NSLog(@"%@",responseObject);
              }
          }

          failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {

              NSLog(@"%@",error.localizedDescription);
          }];
    }

目前,我只发送一张图像进行测试.并得到responseObject,它是字节数据.

For now I only send one image to test. And it get responseObject which is bytes data.

< 696d6167 65313a20 efbfbdef bfbdefbf bdefbfbd 00104a46 49460001 01000048 00480000 efbfbdef bfbd0058 45786966 00004d4d 002a0000 00080002 01120003 00000001 00010000 efbfbd69 00040000 00010000 00260000 00000003 efbfbd01 00030000 00010001 0000efbf bd020004 00000001 0000001e efbfbd03 ...

<696d6167 65313a20 efbfbdef bfbdefbf bdefbfbd 00104a46 49460001 01000048 00480000 efbfbdef bfbd0058 45786966 00004d4d 002a0000 00080002 01120003 00000001 00010000 efbfbd69 00040000 00010000 00260000 00000003 efbfbd01 00030000 00010001 0000efbf bd020004 00000001 0000001e efbfbd03...

根据网络专家的回答,应为图片的网址. Web后端位于ASP.NET中,我在做什么错?

更新
如果更改响应序列化器,则会得到:

As per the web guy response should be the URL of the image. Web backend is in ASP.NET What am I doing wrong?

UPDATE
If I change the response serializer I get:

请求失败:不可接受的内容类型:application/octet-stream

Request failed: unacceptable content-type: application/octet-stream

我什至尝试过:

NSString *string = [NSString stringWithFormat:@"%@/API/Upload",BaseURLString];
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
[manager setRequestSerializer:[AFHTTPRequestSerializer serializer]];
[manager setResponseSerializer:[AFHTTPResponseSerializer serializer]];
NSError *error;

for(NSData *eachImage in self.fetchedAtt) {

    NSURLRequest *request = [manager.requestSerializer multipartFormRequestWithMethod:@"POST" URLString:string parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
        //NSString *value = @"qux";
        //NSData *data = [value dataUsingEncoding:NSUTF8StringEncoding];

//[formData appendPartWithFormData:eachImage名称:@"image1"]; [formData appendPartWithFileData:eachImage名称:@"image1" fileName:@"test.jpg" mimeType:@"image/jpeg"]; }错误:& error];

//[formData appendPartWithFormData:eachImage name:@"image1"]; [formData appendPartWithFileData:eachImage name:@"image1" fileName:@"test.jpg" mimeType:@"image/jpeg"]; } error:&error];

    NSURLSessionDataTask *task = [manager dataTaskWithRequest:request uploadProgress:nil downloadProgress:nil completionHandler:^(NSURLResponse * _Nonnull response, id  _Nullable responseObject, NSError * _Nullable error) {
        if (error) {
            NSLog(@"%@", error.localizedDescription);
            return;
        }

        NSLog(@"%@", responseObject);
    }];
    [task resume];
}

但是得到了Request failed: internal server error (500).如果我取消注释appendPartWithFormData并注释另一行,则在responseObject中得到相同的字节.

更多更新:
我们在邮递员上对其进行了测试,并且图像已成功上传.但是,如果我在参数中给出一些内容,我会得到

But got Request failed: internal server error (500). if I uncomment the appendPartWithFormData and comment other line I get same bytes in responseObject.

More Updates:
We tested it on postman and the image was uploaded successfully. But if I give something in parameter I get

请求失败:不支持的媒体类型(415)

Request failed: unsupported media type (415)

推荐答案

上载图像时,您会期望服务器做出一些响应(只是为了确认已成功接收到它们).话虽如此,看着您的十六进制数据,这很好奇.看起来像是:

When you upload images, you'd expect some response from the server (just to acknowledge that they were received successfully). Having said that, looking at your hex data, it's curious. What it looks like is:

"JFIF"和"Exif"引用与JPEG响应一致. image1看起来像是您发送的图像的名称.但这是一个非常奇怪的反应.绝对不是图片的网址.

The "JFIF" and "Exif" references are consistent with a JPEG response. And the image1 looks like it was the name of the image you sent. But this is a very curious response. It's definitely not the URL of the image.

您应该与Web服务作者联系,并获取他们确切发送回的详细信息,因为它不符合标准响应(XML,JSON等).

You should contact the web service authors and get details of what precisely they're sending back, because it doesn't conform to standard responses (XML, JSON, etc.).

顺便说一句,您上传图像的方法看起来不正确.您通常会执行以下操作:

By the way, your method of uploading the image looks incorrect. You'd usually do something like:

[formData appendPartWithFileData:eachImage
                            name:@"image1"
                        fileName:@"test.jpg"
                        mimeType:@"image/jpeg"];

很明显,您要指定fileNamemimeType来匹配NSData的实际值.而且我会与Web服务的作者再次确认他们是否真的想使用image1的字段名称...这有点不典型.

Clearly, you'd specify the fileName and mimeType to match what the NSData really was. And I'd double check with the author of the web service whether they really want to use a field name of image1 ... that's a little atypical.

这篇关于AFNetworking 3 AFMultipartFormData获取字节数据作为响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆