【发布时间】:2020-02-29 00:57:56
【问题描述】:
我在我的Ubuntu 18.04 上使用Visual Studio Code 版本1.42。我刚刚通过终端成功安装了sudo dotnet add package Google.Apis.Drive.v3,但我找不到在我的C# 项目上安装System.Web 的方法。
在做了一些研究之后,我发现了这个,它解释了基本上,我需要访问一个IHostingEnvironment env object,ASP.NET Core 会负责处理。
问题我无法解析本地文件路径。我的意思是我不熟悉这种方法,想问一下,鉴于下面的代码,有人可以告诉我如何修改它以使用IHostingEnvironment env object。
问题出现在包含以下内容的行中:
HttpContext.Current.Server.MapPath("~/GoogleDriveFiles")
这是剩下的代码:
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading;
//using System.Web;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
namespace WebApi2.Models
{
public class GoogleDriveFilesRepository
{
//defined scope.
public static string[] Scopes = { DriveService.Scope.Drive };
// Operations....
//create Drive API service.
public static DriveService GetService()
{
//Operations....
public static List<GoogleDriveFiles> GetDriveFiles()
{
// Other operations....
}
//file Upload to the Google Drive.
public static void FileUpload(HttpPostedFileBase file) // <-- Error here
{
if (file != null && file.ContentLength > 0)
{
DriveService service = GetService();
string path = Path.Combine(HttpContext.Current.Server.MapPath("~/GoogleDriveFiles"), // <-- Error here
Path.GetFileName(file.FileName));
file.SaveAs(path);
var FileMetaData = new Google.Apis.Drive.v3.Data.File();
FileMetaData.Name = Path.GetFileName(file.FileName);
FileMetaData.MimeType = MimeMapping.GetMimeMapping(path); // <-- Error here
FilesResource.CreateMediaUpload request;
using (var stream = new System.IO.FileStream(path, System.IO.FileMode.Open))
{
request = service.Files.Create(FileMetaData, stream, FileMetaData.MimeType);
request.Fields = "id";
request.Upload();
}
}
}
//Download file from Google Drive by fileId.
public static string DownloadGoogleFile(string fileId)
{
DriveService service = GetService();
string FolderPath = System.Web.HttpContext.Current.Server.MapPath("/GoogleDriveFiles/"); // <-- Error here
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;
}
case DownloadStatus.Failed:
{
Console.WriteLine("Download failed.");
break;
}
}
};
request.Download(stream1);
return FilePath;
}
}
}
到目前为止我做了什么:
1) 我通过this post 作为基本解释,但这并没有解决我遇到的问题。
2) This post 作为基本方法在某种程度上也很有用。有用,但仍然无法弄清楚我缺少什么。
3) 我深入研究了这个问题并到达了here,但仍然没有运气。
非常感谢您指出正确的方向。
【问题讨论】:
-
这里有一些帮助。 andrewlock.net/…
-
您不需要需要
System.Web或IHostingEnvironment才能使用Google 云端硬盘。你想做什么? PS:编辑您的问题并明确问题是什么。现在那些Error herecmets 被完全隐藏了 -3 页,而且离右边太远了 -
您的真正问题是如何解析本地文件路径,而不是如何使用
System.Web或IHostingEnvironment。 -
在您的代码中是否存在您试图“获取”的静态文件..托管在您的网站上?是这个问题吗?
标签: c#