0

I got an error like this when trying to add google drive service to my project. Although there is "System.Web" in the "Library" section, it cannot be used actively. Could you help?

public static string DownloadGoogleFile(string fileId)
{
    DriveService service = GetService();

    string FolderPath = System.Web.HttpContext.Current.Server.MapPath("/GoogleDriveFiles/");
    FilesResource.GetRequest request = service.Files.Get(fileId);

    string FileName = request.Execute().Name;
    string FilePath = System.IO.Path.Combine(FolderPath, FileName);

    MemoryStream stream1 = new MemoryStream();

    // Add a handler which will be notified on progress changes.
    // It will notify on each chunk download and when the
    // download is completed or failed.
    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;
                }
            case DownloadStatus.Failed:
                {
                    Console.WriteLine("Download failed.");
                    break;
                }
        }
    };
    request.Download(stream1);
    return FilePath;
}
  • I am not sure you are using asp.net core or .Net, If you are using asp.net core, You need to use DI to get the httpcontext in your project, You can refer to this [link](https://stackoverflow.com/questions/37329354/how-to-use-ihttpcontextaccessor-in-static-class-to-set-cookies) – Xinran Shen Aug 18 '22 at 09:01

1 Answers1

0

Trying to pass the current HttpContext to a static method gets tricky depending on the project framework. It's also not clear the type of project or framework you are using.

Here's a similar question that might help you clarify the difference between HttpContext when it pertains to .net and .net-core.

HttpContext in .net standard library

Ramp2010
  • 76
  • 4
  • No problem, if you feel this adequately answered your question please consider marking it the accepted answer. Good luck with your project. – Ramp2010 Aug 18 '22 at 14:39