【问题标题】:why filename will return nil in alamo fire uploading images in swift 3?为什么文件名会在 alamo fire 在 swift 3 中上传图片时返回 nil?
【发布时间】:2018-02-12 07:31:53
【问题描述】:

我有时想在我的项目中使用 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!")

                                }


                            })

由于文件名,应用程序将在这一行崩溃

multipartFormData.append(UIImageJPEGRepresentation(UploadingImagesViewController.imageUpload[i], 1.0)!, withName: "myfile", fileName: "file.jpeg", mimeType: "image/jpeg")

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

【问题讨论】:

  • 您是否在上传功能中设置了标头值?
  • 是的,请稍等,我会将其添加到我的代码中

标签: ios swift3 alamofire filenames image-uploading


【解决方案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) 数据值。可能会有帮助。

【讨论】:

  • 由于 CompletionHandler,应用程序不允许我运行
  • 你需要检查 (value as AnyObject).data(using: String.Encoding.utf8.rawValue)!使用前的数据值
  • 我使用了它,当图像数据为零时,它将使用:String.Encoding.utf8.rawValue)!但是当我将图像上传到服务器时,服务器只显示文本文件
  • 您只需要检查图像数据值而不是 nil 。您正在使用 UIImageJPEGRepresentation(imageUploadingViewController.imageUpload[i] 获取 imagedata 值。如果图像数据值不为零并且您的服务器发送文本文件,这是服务器端问题。
  • 它可以正常工作,但有时不能正常上传一些图片,但在我使用你的代码后没有崩溃,谢谢
猜你喜欢
  • 1970-01-01
  • 2018-01-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-08
  • 1970-01-01
相关资源
最近更新 更多