【问题标题】:Google Drive API v3 VB.NET Upload To Spesific FolderGoogle Drive API v3 VB.NET 上传到特定文件夹
【发布时间】:2020-01-19 16:10:00
【问题描述】:

请帮助,我正在尝试使用 Vb.net 将文件上传到我的谷歌驱动器中的特定文件夹。但是,我在谷歌上搜索了几个小时并没有得到工作代码。我无法入睡,因为这个。这是我的代码:

    Private Sub UploadFile(FilePath As String)
        Try
            If Service.ApplicationName <> "Google Drive VB Dot Net" Then CreateService()
            Dim TheFile As New File()
            TheFile.Name = "Database Sekretariat.accdb"
            TheFile.Description = "A test document"
            'TheFile.MimeType = "text/plain"
            TheFile.Parents(0) = "1uMeTMRtvhm5_98udPmV8kp19aGtrmeQj"
            Dim ByteArray As Byte() = System.IO.File.ReadAllBytes(FilePath)
            Dim Stream As New System.IO.MemoryStream(ByteArray)
            Dim UploadRequest As FilesResource.CreateMediaUpload = Service.Files.Create(TheFile, Stream, TheFile.MimeType)
            UploadRequest.Upload()
            Dim file As File = UploadRequest.ResponseBody
            MsgBox("Upload Selesai " & file.Name & "")
        Catch ex As Exception
            MsgBox("Upload Gagal")
        End Try
    End Sub

【问题讨论】:

  • 您的父级设置看起来应该对我有用您遇到了什么错误?
  • 正如@DaImTo 所说,请提供您遇到的错误。另外,您之前是否正确执行过 OAuth 流程?

标签: vb.net google-api google-drive-api google-api-dotnet-client


【解决方案1】:

按如下方式使用,它是功能性的

首先,我查找 ID 文件夹

Public Sub SearchFolder()
    IDFolderSave = String.Empty 'Global Variable
    Try
        Dim findrequest As FilesResource.ListRequest = Service.Files.List
        Dim listFolder As Data.FileList = findrequest.Execute

        For Each item As File In listFolder.Files
            If item.MimeType = "application/vnd.google-apps.folder" Then
                If item.Name = "NameFolder" Then
                    IDFolderSave = item.Id.ToString
                    Exit For
                End If
            End If
        Next
    Catch ex As Exception
        Throw ex
    End Try

    'MsgBox("Folder id: " + idFolder)
End Sub

二、上传文件

Public Sub UploadFileInFolder()
    Dim FilePath As String = String.Empty
    FilePath = "C:\Icons\Loadding.gif"
    Dim plist As List(Of String) = New List(Of String)

    SearchFolder()
    plist.Add(IDFolderSave) 'Set parent folder

    If (System.IO.File.Exists(FilePath)) Then
        Dim fileMetadata = New File() With {
                .Name = "Test",
                .Parents = plist
            }

        Dim request As FilesResource.CreateMediaUpload

        Using stream = New System.IO.FileStream(FilePath, System.IO.FileMode.Open)
            request = Service.Files.Create(fileMetadata, stream, "application/octet-stream")
            request.Fields = "id, parents"
            request.Upload()
        End Using


        Dim file As File = request.ResponseBody
        IDFileShared = file.Id

        SharedFile()
        MsgBox("File upload: " + file.Id)
    Else
        MsgBox("File does not exist: " + FilePath)
    End If
End Sub

希望对你有帮助

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多