Swift将音频文件上传到Firebase

时间:2017-05-15 10:13:10

标签: ios swift firebase firebase-storage

您好我尝试将m4a文件上传到Firebase存储但总是收到错误“Error Domain = FIRStorageErrorDomain Code = -13000”发生了未知错误,请检查服务器响应。“ ResponseBody =无法完成上传。目前的规模是38298.预计最终规模为34554。

这是我的代码:

func uploadSound(localFile: URL) {
    let storageRef = FIRStorage.storage().reference()
    let imagesRef = storageRef.child("upload")
    let fileName = "/" + self.deviceID + ".m4a"
   let uploadTask = spaceRef.putFile(localFile, metadata: nil) { metadata, error in
        if let error = error {
            print(error)
        } else {
            // Metadata contains file metadata such as size, content-type, and download URL.
            let downloadURL = metadata!.downloadURL()
        }
    }
}

感谢帮助..谢谢

2 个答案:

答案 0 :(得分:0)

您创建以下变量,这两个变量都未使用:

let imagesRef = storageRef.child("upload")
let fileName = "/" + self.deviceID + ".m4a"

上传时,您正在致电:

// where is `spaceRef`?
spaceRef.putFile(localFile, metadata: nil)

作为建议,也许可以尝试:

let newFileRef = imagesRef.child(fileName)
let uploadTask = newFileRef.putFile(localFile, metadata: nil) { metadata, error in
        if let error = error {
            print(error)
        } else {
            // Metadata contains file metadata such as size, content-type, and download URL.
            let downloadURL = metadata!.downloadURL()
        }
    }

答案 1 :(得分:0)

你也必须发送元数据和文件作为数据,试试这个。对于 swift5

let metadata = StorageMetadata()
    metadata.contentType = "audio/m4a"
    let riversRef = Storage.storage().reference().child("message_voice").child("\(self.getDate()).m4a")
    do {
        let audioData = try Data(contentsOf: recorder.url)
        riversRef.putData(audioData, metadata: metadata){ (data, error) in
            if error == nil{
                debugPrint("se guardo el audio")
                riversRef.downloadURL {url, error in
                    guard let downloadURL = url else { return }
                    debugPrint("el url descargado", downloadURL)
                }
            }
            else {
                if let error = error?.localizedDescription{
                    debugPrint("error al cargar imagen", error)
                }
                else {
                    debugPrint("error de codigo")
                }
            }
        }
    } catch {
        debugPrint(error.localizedDescription)
    }