从 ASP.NET 下载 Google 驱动文件到用户浏览器的默认对话框
Download Google Drive file from ASP.NET to user's browser default dialog
我正在尝试使用 ASP.NET、MVC、Google Drive API V2 将 Google Drive 文件下载到用户的默认下载文件夹,通过服务帐户进行身份验证.
我看到的示例使用了 _saveTo 路径和文件名。我不希望用户在开始下载之前必须指定此信息。我只希望用户单击网络应用程序中的文件并让它显示浏览器下载对话框。这可能吗?
这是 3 层:
CSHTML
<td>
<a href="@Url.Action("DownloadFile", "GoogleDrive", new { _fileId = item.DriveId })">@Html.DisplayFor(x => item.FileName)</a>
</td>
控制器
[HttpGet]
public ActionResult DownloadFile(string _fileId)
{
GoogleDrivePageVM vm = new GoogleDrivePageVM();
// Get Service
DriveService _service = GoogleDrive.GoogleDriveAPI.GoogleDriveAuthentication();
// Download File
File _file = GoogleDrive.GoogleDriveAPI.GetFile(_service, _fileId);
bool _success = GoogleDrive.GoogleDriveAPI.DownloadFile(_service, _file, null); // _saveTo);
return RedirectToAction("GoogleDriveList", "GoogleDrive");
}
服务层
public static Boolean DownloadFile(DriveService _service, File _file, string _saveTo)
{
if (!String.IsNullOrEmpty(_file.DownloadUrl))
{
try
{
var x = _service.HttpClient.GetByteArrayAsync(_file.DownloadUrl);
byte[] arrBytes = x.Result;
System.IO.File.WriteAllBytes("c://myFirstDownload.txt", arrBytes); //_saveTo
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;
}
}
我觉得我正在尝试实施错误的方法。有人可以澄清这是否可能并指出我正确的方向。我可以只使用网页上的文件下载 URL 吗?
您要查找的下载 URL 名为 webContentlink in Drive API which is generated using files.get。
webContentLink A link for downloading the content of the file in a browser. This is only available for files with binary content in
Drive.
此 link 将允许您直接从浏览器下载文件。
现在使用Try-it进行测试。
我正在尝试使用 ASP.NET、MVC、Google Drive API V2 将 Google Drive 文件下载到用户的默认下载文件夹,通过服务帐户进行身份验证.
我看到的示例使用了 _saveTo 路径和文件名。我不希望用户在开始下载之前必须指定此信息。我只希望用户单击网络应用程序中的文件并让它显示浏览器下载对话框。这可能吗?
这是 3 层:
CSHTML
<td>
<a href="@Url.Action("DownloadFile", "GoogleDrive", new { _fileId = item.DriveId })">@Html.DisplayFor(x => item.FileName)</a>
</td>
控制器
[HttpGet]
public ActionResult DownloadFile(string _fileId)
{
GoogleDrivePageVM vm = new GoogleDrivePageVM();
// Get Service
DriveService _service = GoogleDrive.GoogleDriveAPI.GoogleDriveAuthentication();
// Download File
File _file = GoogleDrive.GoogleDriveAPI.GetFile(_service, _fileId);
bool _success = GoogleDrive.GoogleDriveAPI.DownloadFile(_service, _file, null); // _saveTo);
return RedirectToAction("GoogleDriveList", "GoogleDrive");
}
服务层
public static Boolean DownloadFile(DriveService _service, File _file, string _saveTo)
{
if (!String.IsNullOrEmpty(_file.DownloadUrl))
{
try
{
var x = _service.HttpClient.GetByteArrayAsync(_file.DownloadUrl);
byte[] arrBytes = x.Result;
System.IO.File.WriteAllBytes("c://myFirstDownload.txt", arrBytes); //_saveTo
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;
}
}
我觉得我正在尝试实施错误的方法。有人可以澄清这是否可能并指出我正确的方向。我可以只使用网页上的文件下载 URL 吗?
您要查找的下载 URL 名为 webContentlink in Drive API which is generated using files.get。
webContentLink A link for downloading the content of the file in a browser. This is only available for files with binary content in Drive.
此 link 将允许您直接从浏览器下载文件。
现在使用Try-it进行测试。