【问题标题】:upload Video File to Firebase Storage - Swift将视频文件上传到 Firebase 存储 - Swift
【发布时间】:2020-12-10 09:49:39
【问题描述】:

我正在尝试将视频文件上传到 Firebase 存储,但我不断收到错误消息:“发生未知错误,请检查服务器响应。”但是,我确信我的服务器响应很好,因为我可以毫无问题地上传图片。

这就是我获取视频网址的方式(通过 uiimagepickercontroller):

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
     guard let mediaInfo = info[.mediaType] else { return }
     let mediaType = "\(mediaInfo)"
     if mediaType == "public.movie" {
        // how we handle it if it's a video
        guard let videoURL = info[.mediaURL] as? NSURL else {return }
        videoUrl = videoURL
   }
}

我如何尝试上传视频:

        guard let videoUrl = videoUrl else { return }
        
        let videoName = NSUUID().uuidString
        let storageRef = Storage.storage().reference().child("\(videoName).mov")
        storageRef.putFile(from: videoUrl as URL, metadata: nil) { (metaData, error) in
             // IMPORTANT: this is where I got the error from 
            if error != nil {
                print("error uploading video: \(error!.localizedDescription)")
            } else {
                // successfully uploaded the video
                storageRef.downloadURL { (url, error) in
                    if error != nil {
                        print("error downloading uploaded videos Url: \(error!.localizedDescription)")
                    } else {
                        if let downloadUrl = url {
                            let contentType = "videoUrl" //just a var for the following func
                            self.uploadPost(for: downloadUrl, contentType: contentType) // func that uploads the url to the database
                        }
                    }
                }
            }
        }

有人可以帮我解释为什么这不起作用,也许我需要将 url 转换为 mp4 文件或其他东西。提前致谢!

【问题讨论】:

    标签: swift xcode firebase video upload


    【解决方案1】:

    我找到了答案,它真的很简单...... 发生这种情况的原因是因为视频 url 实际上不是“带有路径 Url 的文件” 所以解决方案很简单,在 didFinishPicking 中:

    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
     guard let mediaInfo = info[.mediaType] else { return }
     let mediaType = "\(mediaInfo)"
     if mediaType == "public.movie" {
        // how we handle it if it's a video
        guard let videoURL = info[.mediaURL] as? NSURL else {return }
        videoUrl = videoURL.filePathURL as? NSURL // this is the only thing I changed cause this is the file we are uploading
      }
    }
    

    感谢所有的帮助!即使它没有任何帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-10-14
      • 2018-06-21
      • 2020-12-19
      • 1970-01-01
      • 2019-01-20
      • 2020-10-26
      • 1970-01-01
      • 2020-09-08
      相关资源
      最近更新 更多