使用网址swift Alamofire

时间:2018-04-04 11:01:00

标签: ios swift alamofire

我需要使用alamofire将视频上传到服务器。用户选择视频,我在didFinishPickingMediaWithInfo中成功获取URL,如下所示:

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
        picker.dismiss(animated: true, completion: nil)
        if let pickedVideo = info[UIImagePickerControllerMediaURL] as? URL {
            print(pickedVideo)
        }
    }

然后我使用以下代码上传视频:

Alamofire.upload( multipartFormData: { multipartFormData in
            multipartFormData.append(videoUrl, withName: "video", fileName: "video.mp4", mimeType: "video/mp4")

        }, to: url, encodingCompletion: { encodingResult in
            switch encodingResult {
            case .success(let upload, _, _):
                upload.responseJSON { response in
                    if let JSON = response.result.value as? NSDictionary {
                        completion(true)
                    } else {
                        completion(false)
                        print(response)
                    }
                }
            case .failure(let encodingError):
                print(encodingError)
                completion(false)
            }
        })

它进入故障块,并显示以下错误:

  

multipartEncodingFailed(Alamofire.AFError.MultipartEncodingFailureReason.bodyPartFileNotReachableWithError(atURL:   文件:/private/var/mobile/Containers/Data/Application/C662AB0E-6A4F-40FB-9949-7F0A5AA2BA49/tmp/52C86F07-5DCC-413A-9F8C-71BBF33F793C.MOV    - file:///,error:Error Domain = NSCocoaErrorDomain Code = 260“无法打开文件”52C86F07-5DCC-413A-9F8C-71BBF33F793C.MOV“,因为   没有这样的文件。

2 个答案:

答案 0 :(得分:2)

您正试图通过网址上传视频,这是不可能的,multipartFormData需要数据上传而不是网址,因此首先将其转换为Data然后上传。

show imagePickerController仅用于视频的功能:

func showImagePicker(){
        let picker = UIImagePickerController()
        picker.delegate = self
        picker.mediaTypes = [kUTTypeMovie as String]
        self.present(picker, animated: true, completion: nil)
    }

UIImagePickerControllerDelegate功能,在选择视频后工作:

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
            picker.dismiss(animated: true, completion: nil)

            guard let videoUrl = info[UIImagePickerControllerMediaURL] as? URL else {
                return
            }
            do {
                let data = try Data(contentsOf: videoUrl, options: .mappedIfSafe)
                print(data)
//  here you can see data bytes of selected video, this data object is upload to server by multipartFormData upload
            } catch  {
            }
        }

答案 1 :(得分:0)

使用Alamofire 5,您可以执行以下操作将视频上传到服务器:

 import Alamofire

 func uploadVideo(videoUrl: URL) { // local video file path..
        let timestamp = NSDate().timeIntervalSince1970 // just for some random name.

        AF.upload(multipartFormData: { (multipartFormData) in
            multipartFormData.append(videoUrl, withName: "image", fileName: "\(timestamp).mp4", mimeType: "\(timestamp)/mp4")
        }, to: endPoint!  ).responseJSON { (response) in
            debugPrint(response)
        }
    }
  

注意:endPoint是一个字符串。示例:http://172.10.3.7:5000/uploadvideo