【发布时间】:2020-10-28 14:32:29
【问题描述】:
我可以在本地服务器上成功下载文件,但部署后我不能在不下载的情况下继续加载
<a href="~/Home/DownloadFile/@item.Id">Download</a>
HomeController.cs
public void DownloadFile(string id)
{
string FilePath = DownloadGoogleFile(id);
Response.AddHeader("Content-Disposition", "attachment; filename=" + Path.GetFileName(FilePath));
Response.WriteFile(System.Web.Hosting.HostingEnvironment.MapPath("/GoogleDriveFiles/" + Path.GetFileName(FilePath)));
Response.End();
Response.Flush();
}
static string DownloadGoogleFile(string fileId)
{
Google.Apis.Drive.v3.DriveService service = GetService();
string FolderPath = System.Web.Hosting.HostingEnvironment.MapPath("/GoogleDriveFiles/");
Google.Apis.Drive.v3.FilesResource.GetRequest request = service.Files.Get(fileId);
string FileName = request.Execute().Name;
string FilePath = System.IO.Path.Combine(FolderPath, FileName);
MemoryStream stream1 = new MemoryStream();
request.MediaDownloader.ProgressChanged += (Google.Apis.Download.IDownloadProgress progress) =>
{
switch (progress.Status)
{
case DownloadStatus.Downloading:
{
Console.WriteLine(progress.BytesDownloaded);
break;
}
case DownloadStatus.Completed:
{
Console.WriteLine("Download complete.");
SaveStream(stream1, FilePath);
break;
}
}
};
request.Download(stream1);
return FilePath;
}
public static Google.Apis.Drive.v3.DriveService GetService()
{
var CSPath = System.Web.Hosting.HostingEnvironment.MapPath("~/");
UserCredential credential;
using (var stream = new FileStream(Path.Combine(CSPath, "client_secret.json"), FileMode.Open, FileAccess.Read))
{
String FilePath = Path.Combine(CSPath, "DriveServiceCredentials.json");
credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.Load(stream).Secrets,
Scopes,
"user",
CancellationToken.None,
new FileDataStore(FilePath, true)).Result;
}
Google.Apis.Drive.v3.DriveService service = new Google.Apis.Drive.v3.DriveService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = "GoogleDriveRestAPI-v3",
});
return service;
}
我应该改变什么?我正在尝试从谷歌获取文件,而不是从浏览器获取文件是 SSL 证书,并且确保网络与此有关,因为我的网络现在不安全?
【问题讨论】:
-
请提供您的授权码和您的 GetService() 方法
-
我更新代码
标签: c# asp.net asp.net-mvc google-drive-api google-api-dotnet-client