为什么我不能将文件作为Alamofire中的参数值之一附加?

时间:2022-08-02 21:03:27

I want to know why can't I give a file as one of the parameter values and to send the parameter as an upload request in Alamofire. I tried like this and it works.

我想知道为什么我不能将文件作为参数值之一,并将参数作为上传请求发送到Alamofire。我试过这样,它的确有效。

let parameters: [String : AnyObject ] = [
        "email" : "abc@gmail.com",
        "password" : "password",
        "full_name": "XXX"]

let image: UIImage? = UIImage(named: "logo.png")

    Alamofire.upload(
        .POST,
        "http://myurl.com/register",
        multipartFormData: { multipartFormData in

            for (key, value) in parameters {
                multipartFormData.appendBodyPart(data: value.dataUsingEncoding(NSUTF8StringEncoding)!, name: key)
              }

                if let imageData = UIImageJPEGRepresentation(image!, 1) {
                multipartFormData.appendBodyPart(data: imageData, name: "profile_pic", fileName: "logo.png", mimeType: "image/png")
              }
       },
        encodingCompletion: { encodingResult in
            switch encodingResult {
            case .Success(let upload, _, _):
                upload.responseJSON { response in
                    debugPrint(response)
                }
            case .Failure(let encodingError):
                print(encodingError)
            }
        }
    )

Why can't I pass the image as a parameter value? And also why should I need to change the image in UIImageJPEGRepresentation.

为什么我不能将图像作为参数值传递?还有为什么我需要在UIImageJPEGRepresentation中更改图像。

If I pass the image as a value without changing the representation, error throws that it is not in NSData format.

如果我将图像作为值传递而不更改表示,则错误会抛出它不是NSData格式。

Note that the request should pass even if there is an image or not, because it is an optional one.

请注意,即使存在图像,请求也应该通过,因为它是可选的。

1 个解决方案

#1


0  

As you know, Alamofire just a library to help you to process a NSURLRequest. This is a common way that client can communicate with server via a HTTP request. Other platforms (such as Android, Desktop, or Web...) also use this way, too. So they must be same format. The post body only accepts simple data format such as String, Int, or byte[]... It does not know what is UIImage, but NSData. NSData is a format of byte[].

如您所知,Alamofire只是一个帮助您处理NSURLRequest的库。这是客户端可以通过HTTP请求与服务器通信的常用方法。其他平台(例如Android,桌面或Web ......)也使用这种方式。所以他们必须是相同的格式。 post body只接受简单的数据格式,如String,Int或byte [] ...它不知道什么是UIImage,而是NSData。 NSData是byte []的格式。

So, if you want to upload a text file, or a video, you have to convert it to NSData, too.

因此,如果要上传文本文件或视频,则必须将其转换为NSData。

Hope this help.

希望这有帮助。

#1


0  

As you know, Alamofire just a library to help you to process a NSURLRequest. This is a common way that client can communicate with server via a HTTP request. Other platforms (such as Android, Desktop, or Web...) also use this way, too. So they must be same format. The post body only accepts simple data format such as String, Int, or byte[]... It does not know what is UIImage, but NSData. NSData is a format of byte[].

如您所知,Alamofire只是一个帮助您处理NSURLRequest的库。这是客户端可以通过HTTP请求与服务器通信的常用方法。其他平台(例如Android,桌面或Web ......)也使用这种方式。所以他们必须是相同的格式。 post body只接受简单的数据格式,如String,Int或byte [] ...它不知道什么是UIImage,而是NSData。 NSData是byte []的格式。

So, if you want to upload a text file, or a video, you have to convert it to NSData, too.

因此,如果要上传文本文件或视频,则必须将其转换为NSData。

Hope this help.

希望这有帮助。