【问题标题】:Swift uploading image, response from server says file wasn't an image?快速上传图片,服务器响应说文件不是图片?
【发布时间】:2023-03-08 20:57:01
【问题描述】:

处理一个 Swift 项目,显然我上传的文件不是图像。我也在使用 Alamofire 实用程序。

let uuid = NSUUID().UUIDString
    print ("MARK -- UUID is " + uuid)
    let image = imageView.image

    Alamofire.upload(.POST, "{RETRACTED}", multipartFormData: {
        multipartFormData in
        if  let imageData = UIImageJPEGRepresentation(image!, 0.6) {
            multipartFormData.appendBodyPart(data: imageData, name: uuid, fileName: uuid + ".png", mimeType: "image/png")
        }
        }, encodingCompletion: {
            encodingResult in

            switch encodingResult {
            case .Success(let upload, _, _):
                upload.responseJSON { response in
                    print(response.request)  // original URL request
                    print(response.response) // URL response
                    print(response.data)     // server data
                    print(response.result)   // result of response serialization

                    if let JSON = response.result.value {
                        print ("JSON \(JSON)")
                        self.displayAlert("Uploaded!", message: "{RETRACTED}", responseButtonText: "{RETRACTED}")
                    }
                }

            case .Failure(let encodingError):
                print(encodingError)
            }
    })

这是 JSON 响应(如果需要,我还有 PHP 服务器端代码):

JSON { response = "该文件不是图像(仅接受 .png 和 .jpg/.jpeg 图像)。类型为 "; 响应代码 = 0; }

谢谢大家。

【问题讨论】:

    标签: php json swift alamofire


    【解决方案1】:

    您发送的是 jpg 图像,但指定了 png 的 mime 类型。将 mime 类型更改为“image/jpeg”或发送 png 文件。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-01
      相关资源
      最近更新 更多