ios - 无法使用 Alamofire 多部分上传相机图像

标签 ios swift image-upload alamofire-request

我必须上传图像、音频、文档。我正在使用 Alamofire 上传。所有内容都已上传,包括屏幕截图等图库图像,但从相机拍摄的照片未上传。 这是我的代码:

func requestUploadFileWithMultipart(connectionUrl: String, param : [String: AnyObject], filePath: String?, _ callBack: @escaping (_ data: DataResponse<Any>?, _ error:Error?) -> Void) {

    let URLString  = MainURL + connectionUrl

    Alamofire.upload(multipartFormData: { multipartFormData in
        for (key, value) in param {
            let stringValue = "\(value)"
            multipartFormData.append(stringValue.data(using: String.Encoding.utf8)!, withName: key)
            print("Key: \(key), Value: \(stringValue)")
        }

        if filePath != "" {
            do {
                var fileData = try Data(contentsOf: URL(string: filePath!)!)
                let ext = URL(fileURLWithPath: filePath!).lastPathComponent.components(separatedBy: ".").last
                let mime = filePath?.mimeTypeForPath()
                let fileName = "\(Date().timeIntervalSince1970)".components(separatedBy: ".").first
                multipartFormData.append(fileData, withName: "file", fileName: "\(fileName ?? "file").\(ext ?? "")", mimeType: mime ?? "")
            } catch {
                print("error loading file in multipart")
            }
        }

    }, to:URLString) { (result) in
        switch result {
        case .success(let upload, _, _):
            upload.uploadProgress(closure: { (progress) in
                print("Upload Progress: \(progress.fractionCompleted)")
            })

            upload.responseJSON { response in
                print(response.result.value as Any)



                callBack(response, nil)
            }

        case .failure(let encodingError):
            print(encodingError)

            callBack(nil, encodingError)
        }
    }
}

最佳答案

图像压缩对我有用。可能是大文件没有上传。

关于ios - 无法使用 Alamofire 多部分上传相机图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57005053/

相关文章:

ios - 使用 AWS SDK v2 (iOS) 连接到 S3

xcode - 复制/粘贴时 View Controller 变小(Xcode 7、Swift 2)

php - 将图像上传到服务器并存储在MySQL数据库中

ASP.NET : A generic error occurred in GDI+

iphone - 我们可以使用 AVCaptureSession 启用相机控件吗?

ios - 我的背景音乐被播放了两次(同时)

ios - UIView:在显示之前调用了哪个方法?

iOS Swift 4 - Twitter 登录按钮不执行任何操作 - 返回线程 1 Sigabrt,没有堆栈跟踪

ios - 如果我们使用多个文本字段,如何自动获取 OTP