如何使用VB.NET将文件添加到Google云端硬盘的子文件夹

问题描述 投票:0回答:1

我花了数小时试图将子文件夹添加到Google驱动器,并且还向子文件夹添加了文件,但是徒劳。下面的代码可以将文件上传到驱动器,但是我无法添加对子集文件夹的引用以添加到其中。我已经提到了下面的链接,但是不知道如何将下面的行转换为vb.net。任何帮助将不胜感激。

body.Parents = New List<ParentReference>() { New ParentReference() { Id = _parent } };

https://developers.google.com/drive/api/v3/folderhttps://developers.google.com/drive/api/v2/reference/files/insert#.net

请帮助。

 Dim vFIle As New File()
        vFIle.Title = "My document.txt"
        vFIle.Description = "A test document"
        vFIle.MimeType = "text/plain"

        Dim ByteArray As Byte() = System.IO.File.ReadAllBytes(FilePath)
        Dim Stream As New System.IO.MemoryStream(ByteArray)

        Dim UploadRequest As FilesResource.InsertMediaUpload = Service.Files.Insert(vFIle, Stream, vFIle.MimeType)
        UploadRequest.Upload()
        Dim file As File = UploadRequest.ResponseBody

---- > test2

Dim filetoUp As String = "C:\install log.txt"
If Service.ApplicationName <> "Google Drive VB Dot Net" Then CreateService()
Dim folderId As String = "1UDFFDxi25Hfdgh34sdefcciRE67qzisdRL"
'Dim FodlersList As New List(Of String) From {folderId, "1UDFFDxi25Hfdgh34sdefcciRE67qzisdRLX1", "1UDFFDxi25Hfdgh34sdefcciRE67qzisdRLX2"}
'Dim plist As New List(Of ParentList) From {folderId}

Dim fileMetadata = New File()
With fileMetadata
    .Title = "new.txt"
    .MimeType = "application/vnd.google-apps.folder"
    '.Parents = New List(Of String) From {folderId}
    .Parents(0) = Service.Files.List(folderId)
End With


'System.IO.FileMode.Open
Dim stream = New System.IO.FileStream(filetoUp, System.IO.FileMode.Open)

Dim request
request = Service.Files.Insert(fileMetadata, stream, "text/plain")
'fields is not valid member
'request.Fields = "id" 
request.Upload()
vb.net google-drive-api
1个回答
0
投票

方法

我们的文件资源具有一个Parents参数,该参数接受ParentReference对象的列表。一个简单的ParentReference对象具有一个Id参数,代表Google云端硬盘中的文件夹ID。

这里您尝试将字符串隐式转换为ParentList

Dim folderId As String = "1UDFFDxi25Hfdgh34sdefcciRE67qzisdRL"
Dim plist As New List(Of ParentList) From {folderId}

我不确定Google Api中是否存在此ParentList类,但这是导致程序失败的原因。

您应该改为创建ParentReference的列表,如下所示:

Dim folderId = "your-folder-id"
Dim TheFile As New File()
TheFile.Title = "My document"
TheFile.Description = "A test document"
TheFile.MimeType = "text/plain"
TheFile.Parents = New List(Of ParentReference) From {New ParentReference() With {.Id = folderId}}

参考

Drive v2 Files

Drive API v2 .Net Guide

© www.soinside.com 2019 - 2024. All rights reserved.