为什么filename会在alamo fire中返回nil在swift 3中上传图片?

时间:2017-09-04 05:18:14

标签: ios swift3 alamofire filenames image-uploading

我想在我的项目中使用alamo fire上传多部分图像,并且没有理由alamo fire文件名将返回nil - 我该如何避免?

let params: Parameters = ["name": "image\(i)"]
                    Alamofire.upload(multipartFormData:
                        {

                            (multipartFormData) in
                            multipartFormData.append(UIImageJPEGRepresentation(imageUploadingViewController.imageUpload[i], 1.0)!, withName: "myfile", fileName: "file.jpeg", mimeType: "image/jpeg")
                            for (key, value) in params
                            {
                                multipartFormData.append((value as AnyObject).data(using: String.Encoding.utf8.rawValue)!, withName: key)

                            }
                    }, to: "http:example.com/api/file?api_token=\(enterViewController.api_token)&id=\(self.postID)",headers:nil)
                    { (result) in
                        switch result {
                        case .success(let upload,_,_ ):
                            upload.uploadProgress(closure: { (progress) in
                                UploadingViewController.progressing = progress.fractionCompleted

                                self.UpdateState()


                                if progress.fractionCompleted == 1.0 {


                                    DispatchQueue.main.async {

                                        UploadingViewController.fine = UploadingViewController.fine + 1

                                    }


                                    print(UploadingViewController.fine)
                                    self.checkUploadProgress()
                                    print("OK Finished!")

                                }


                            })

由于fileName

,应用程序将在此行中崩溃
multipartFormData.append(UIImageJPEGRepresentation(UploadingImagesViewController.imageUpload[i], 1.0)!, withName: "myfile", fileName: "file.jpeg", mimeType: "image/jpeg")

请记住,有时它会起作用我不知道为什么会发生这种情况!

1 个答案:

答案 0 :(得分:1)

func sendImageToServerWithURL(_ URLString: URLConvertible, method: HTTPMethod, headers: [String : String]?, parameters: [String: Any]?, imageData : Data?,imageName:String,completionHandler: @escaping CompletionHandler) {

    Alamofire.upload(multipartFormData: { (multipartFormData) in

        if((imageData) != nil)
        {
             multipartFormData.append(imageData!, withName:imageName, fileName: "swift_file.png", mimeType: "image/png")
        }

        for (key, value) in parameters!
        {
            multipartFormData.append((value as AnyObject).data(using: String.Encoding.utf8.rawValue)!, withName: key)
        }
    }, to:URLString ,headers : headers)
    { (result) in
        switch result {
        case .success(let upload, _, _):

            upload.uploadProgress(closure: { (progress) in
                //Print progress
            })

            upload.responseJSON { response in
                print (response.result)

                completionHandler(response)
            }

        case .failure( _): break
            //print encodingError.description
        }
    }
}

我正在使用该方法上传图片。我在上传之前检查 if((imageData)!= nil)数据值。可能这有用。