[英]How to upload a file to Firebase in an iOS app (Swift)
I used this answer https://stackoverflow.com/a/64168754/16212254 for reference to allow the user to pick a file, but now I am stuck on how to upload the file to Firebase. I'm NEW to Swift, please don't mind my silly question.我使用这个答案https://stackoverflow.com/a/64168754/16212254作为参考,允许用户选择一个文件,但现在我不知道如何将文件上传到 Firebase。我是 Swift 的新手,拜托别介意我的愚蠢问题。
I tried to follow https://firebase.google.com/docs/firestore/quickstart#ios and https://firebase.google.com/docs/storage/ios/upload-files but still could not figure out how to send the file to firebase since I don't know the attached file's name and path.我试图按照https://firebase.google.com/docs/firestore/quickstart#ios和https://firebase.google.com/docs/storage/ios/upload-files但仍然无法弄清楚如何发送将文件发送到 firebase,因为我不知道附件的名称和路径。
The answer you used to be able to pick a file works fine, but doesn't give a reference to the selected image or file outside the scope of the didFinishPickingMediaWithInfo
and didPickDocumentsAt
functions.您过去能够选择文件的答案工作正常,但没有在didFinishPickingMediaWithInfo
和didPickDocumentsAt
函数的 scope 之外提供对所选图像或文件的引用。
Here's how to save your file/image data to be able to access it later.以下是保存文件/图像数据以便日后访问的方法。
Create a variable of type Data?
创建Data?
in your ViewController.在你的 ViewController 中。
var myData: Data?
If you are dealing with an Image, inside the didFinishPickingMediaWithInfo
function, save the png data of the selected image inside the newly created variable (which scope is not limited to the functions itself).如果您正在处理图像,在didFinishPickingMediaWithInfo
function 中,将所选图像的 png 数据保存在新创建的变量中(其中 scope 不限于函数本身)。 If you are instead dealing with a file, do basically the same thing inside the didPickDocumentAt
function.如果您改为处理文件,请在didPickDocumentAt
function 中执行基本相同的操作。
// Image
if let pickedImage = info[UIImagePickerController.InfoKey.originalImage] as? UIImage {
// The following 2 lines don't actually do anything: the selectedImageData dictionary
// ceases existing as soon as the functions ends
// selectedImageData["filename"] = fileUrl.lastPathComponent
// selectedImageData["data"] = pickedImage.pngData()?.base64EncodedString(options: .lineLength64Characters)
self.myData = pickedImage.pngData()
}
// File
do {
let fileData = try Data.init(contentsOf: file.absoluteURL)
// The following 2 lines don't actually do anything: the selectedFileData dictionary
// ceases existing as soon as the functions ends
// selectedFileData["filename"] = file.lastPathComponent
// selectedFileData["data"] = fileData.base64EncodedString(options: .lineLength64Characters)
self.myData = fileData
} catch {
// ...
}
Then, to upload the data to the Firebase Storage, you can use the following function:然后,要将数据上传到 Firebase Storage,可以使用以下 function:
import FirebaseStorage // At the beginning of the file
// Uploads the selected image/file data there is any.
func uploadSavedData() {
guard let data = myData else { return } // Checks whether there is actual data to upload.
let storageRef = Storage.storage().reference()
let fileRef = storageRef.child("userUID/files/documentName.png")
let uploadTask = fileRef.putData(data, metadata: nil) { (metadata, error) in
guard let metadata = metadata else { return } // Cancels task if there is any error
fileRef.downloadURL { (url, error) in {
guard let downloadURL = url else { return }
print(downloadURL) // Prints the URL to the newly uploaded data.
}
}
}
Further resources on the topic有关该主题的更多资源
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.