如何在swift 3中的谷歌硬盘中上传CSV文件

时间:2017-08-17 10:14:31

标签: ios csv swift3 google-drive-api

我正在使用google drive SDK上传CSV文件。我使用谷歌中提供的代码,但错误即将来临

func create(){

    let fileData = FileManager.default.contents(atPath: "FL_insurance_sample.csv")

    let folderId: String = self.fileId!
    let metadata = GTLRDrive_File.init()
    metadata.name = "FL_insurance_sample"
    metadata.mimeType = "application/vnd.google-apps.spreadsheet"
    metadata.parents = [folderId]


    let uploadParameters = GTLRUploadParameters(data: fileData! , mimeType: "text/csv")
    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)")
        }
    })         

}

在fileData上的uploadParameters上显示nil错误。任何人都可以帮助我。

1 个答案:

答案 0 :(得分:0)

变量fileData为nil,因为您传递了错误的文件路径。要获得正确的文件路径使用:

from PyQt5.QtWidgets import *

app = QApplication([])

# store native style
native_style = QCommonStyle()

# set new style globally
s = QStyleFactory.create('Fusion')
app.setStyle(s)

w = QWidget()
w.setFixedSize(300, 200)
l = QVBoxLayout(w)
l.addWidget(QLabel('Label'))
l.addWidget(QPushButton('Button'))
l.addWidget(QProgressBar())

# create progressbar in old style
p = QProgressBar()
p.setStyle(native_style)
l.addWidget(p)

w.show()
app.exec_()