Swift上传图片,来自服务器的响应说文件不是图像?

时间:2016-05-18 01:14:54

标签: php json swift alamofire

使用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图像)。类型为”;     responseCode = 0; }

谢谢你们。

1 个答案:

答案 0 :(得分:0)

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