使用swift 3将文件上传到谷歌硬盘

时间:2018-05-22 07:18:19

标签: ios swift swift3 google-drive-api

我已经创建了文件夹并使用swift 3在我的应用程序中获取了它们 现在我尝试将音频文件上传到选定的文件夹,我开始尝试使用图像,这是我写的代码:

let folderId: String = folders[indexPath.row].folderId
    let metadata = GTLRDrive_File.init()
    metadata.name = "weather"
    metadata.mimeType = "application/vnd.google-apps.photo"
    metadata.parents = [folderId]

    guard let filePath = Bundle.main.path(forResource: "weather", ofType: "png") else {
        return
    }
    guard let fileData = FileManager.default.contents(atPath: filePath) else {

        return
    }

    print("result: \(fileData.count) bytes")


    let imageFromPath = UIImage(contentsOfFile: filePath)!
    selectedimg.image = imageFromPath

    let uploadParameters = GTLRUploadParameters(data: fileData , mimeType: "image/png")
    uploadParameters.shouldUploadWithSingleRequest = true
    let query = GTLRDriveQuery_FilesCreate.query(withObject: metadata, uploadParameters: uploadParameters)
    query.fields = "id"
    self.service.executeQuery(query, completionHandler: {(ticket:GTLRServiceTicket, object:Any?, error:Error?) in
        if error == nil {
            //  print("File ID \(files.identifier)")
        }
        else {
            print("An error occurred: \(error)")
        }
    })

但是当我检查我的驱动器时,图像只是一个空的Google文档,0字节。 我试图从filePath显示图像并打印fileData大小,一切都没问题。 我想帮助解决这个问题,以及如何解决它。 Thanx提前

更新 我设法解决了,我刚刚删除了这一行

metadata.mimeType = "application/vnd.google-apps.photo"

0 个答案:

没有答案