简体   繁体   English

如何在 iOS 应用程序中将文件上传到 Firebase (Swift)

[英]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#ioshttps://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.您过去能够选择文件的答案工作正常,但没有在didFinishPickingMediaWithInfodidPickDocumentsAt函数的 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.

相关问题 如何将图像文件上传到Firebase存储并在Flutter web app中显示 - How to upload an image file to Firebase Storage and show it in the Flutter web app 如何使用 swift 在 firebase 上上传多张图片? - How to upload multiple image on firebase using swift? 如何将 JSON 文件上传到 firebase - How to upload JSON file to firebase iOS Firebase Crashylitics 日志 - 上传 dSYM 文件以处理它们 - iOS Firebase Crashylitics Logs - Upload dSYM file to process them 对于 iOS,我是否必须将同一个应用程序两次上传到 Firebase? - Do I have to upload the same app twice to Firebase for iOS? 链接多个帐户 iOS 应用程序 - Firebase/Facebook 登录和 XCode/Swift 5 - Link multiple accounts iOS app - Firebase/Facebook Login & XCode/Swift 5 iOS/Objective-C:如何在 Firebase Crashlytics 中导入 DWARF 文件? “${PODS_ROOT}/FirebaseCrashlytics/upload-symbols”不再工作 - iOS/Objective-C: How to import DWARF file in Firebase Crashlytics? "${PODS_ROOT}/FirebaseCrashlytics/upload-symbols" not working anymore 如何在 firebase 模拟器中测试文件上传到存储? - How to test file upload into storage in firebase emulator? 如何使用 React 上传 firebase 中的文件? - How to upload file in firebase using React? 我们如何在 ios swift 上对 firebase / firestore 查询进行分页? - How can we paginate a firebase / firestore query on ios swift?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM