0

我正在尝试使用UIDocumentPickerViewController选择 PDF 文件并上传到 Firebase 存储,但不幸的是,我在控制台中不断取得错误的成功。

有谁知道我可能做错了什么?先感谢您。

已选择:file:///private/var/mobile/Library/Mobile%20Documents/com~apple~CloudDocs/Desktop/Documents/document.pdf

协调员离开 可选(错误域=FIRStorageErrorDomain 代码=-13010“对象上传/文档不存在。”

UserInfo={object=uploads/documents, ResponseBody={ "error": { "code": 404, "message": "Not Found. could not get object", "status": "GET_OBJECT" } },

桶=名称-webapp.appspot.com,数据={长度= 115,字节= 0x7b0a2020 22657272 6f72223a 207b0a20 ... 54220a20 207d0a7d},data_content_type=应用程序/json;charset=UTF-8, NSLocalizedDescription=对象上传/文档不存在。, ResponseErrorDomain=com.google.HTTPStatus, ResponseErrorCode=404})

上传图像数据:假,无

文档选择器

class Coordinator: NSObject, UIDocumentPickerDelegate {
    var parent: FilePicker

    init( parent1: FilePicker) {
        self.parent = parent1
    }
    
    func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
        print("Selected: \(urls.first!)")
        
        let localFile = urls.first!

        // User reference URL for home/roomIndex/tenancy
        // Pass to FirestoreManager
        let firestoreManager = FirestoreManager()
        
        firestoreManager.uploadFile(localFile: localFile, serverFileName: "filename.pdf") { (isSuccess, url) in
            
            print("uploadImageData: \(isSuccess), \(url ?? "none")")
            
        }

    }
    
    func documentPickerWasCancelled(_ controller: UIDocumentPickerViewController) {
        print("Cancelled")
    }



    deinit {
        print("Coordinator going away")
    }
}

火力基地

func uploadFile(localFile: URL, serverFileName: String, completionHandler: @escaping (_ isSuccess: Bool, _ url: String?) -> Void) {
    
    let storage = Storage.storage()
    let storageRef = storage.reference()
    // Create a reference to the file you want to upload
    let directory = "uploads"
    let fileRef = storageRef.child(directory).child("documents")

    _ = fileRef.putFile(from: localFile, metadata: nil) { metadata, error in
        fileRef.downloadURL { (url, error) in
            guard let downloadURL = url else {
                // Uh-oh, an error occurred!
                print("\(error)")
                completionHandler(false, nil)
                return
            }
            // File Uploaded Successfully
            completionHandler(true, downloadURL.absoluteString)
        }
    }
}
4

1 回答 1

0

我已经修复了发布。

首先,我记录了fileRef.putFile错误

“文件“...”无法打开,因为您没有查看权限。”</p>

然后我需要在功能中启用 iCloud 文档。之后,错误仍然存​​在,这是由于.upload而不是.import下一行。

 let picker = UIDocumentPickerViewController(documentTypes: [String(kUTTypePDF)], in: .import)
于 2020-06-27T20:14:47.697 回答