如何使用drive api从get请求中获取单个文件名?
我已经提出了请求,但那里没有关于该文件的元数据,我只能下载它。
var fileId = "0BwwA4oUTeiV1UVNwOHItT0xfa2M";
var request = driveService.Files.Get(fileId);
显然,这会根据此doc
在响应中返回files.get我只想下载一个文件并显示其名称,而不仅仅是其ID
答案 0 :(得分:4)
对于Google Drive V3:
C#:
string f = driveService.Files.Get(fileId).Execute().Name;
VB:
Dim f As String = driveService.Files.Get(fileId).Execute().Name
答案 1 :(得分:1)
您可以从Title
类的File
属性中获取文件名:
string FileName = service.Files.Get(FileId).Execute().Title;
和下载,
// DriveService _service: a valid Authendicated DriveService
// Google.Apis.Drive.v2.Data.File _fileResource: Resource of the file to download. (from service.Files.Get(FileId).Execute();)
// string _saveTo: Full file path to save the file
public static void downloadFile(DriveService _service, File _fileResource, string _saveTo)
{
if (!String.IsNullOrEmpty(_fileResource.DownloadUrl))
{
try
{
var x = _service.HttpClient.GetByteArrayAsync(_fileResource.DownloadUrl);
byte[] arrBytes = x.Result;
System.IO.File.WriteAllBytes(_saveTo, arrBytes);
}
catch(Exception e)
{
MessageBox.Show(e.Message, "Error Occured", MessageBoxButtons.OK, MessageBoxIcon.Error);
Environment.Exit(0);
}
}
}
答案 2 :(得分:0)
试试这个
///
/// Download a file
/// Documentation: https://developers.google.com/drive/v2/reference/files/get
///
/// a Valid authenticated DriveService
/// File resource of the file to download
/// location of where to save the file including the file name to save it as.
///
public static Boolean downloadFile(DriveService _service, File _fileResource, string _saveTo)
{
if (!String.IsNullOrEmpty(_fileResource.DownloadUrl))
{
try
{
var x = _service.HttpClient.GetByteArrayAsync(_fileResource.DownloadUrl );
byte[] arrBytes = x.Result;
System.IO.File.WriteAllBytes(_saveTo, arrBytes);
return true;
}
catch (Exception e)
{
Console.WriteLine("An error occurred: " + e.Message);
return false;
}
}
else
{
// The file doesn't have any content stored on Drive.
return false;
}
}
从我的Google drive api C# download教程中删除了代码。我没有更新的年龄,所以如果有任何问题让我知道,我会解决它们。