我正在Asp.net MVC中创建一个应用程序,我想在谷歌驱动器上传文件。以下代码成功创建新文件夹并将文件保存在该文件夹中:
public void Upload(string fileName, byte[] bytes)
{
if (_userCredential != null)
{
var service = new DriveService(new BaseClientService.Initializer
{
HttpClientInitializer = _userCredential,
ApplicationName = "Test App"
});
var file = new File { Title = "Test folder", MimeType = "application/vnd.google-apps.folder" };
var result = service.Files.Insert(file).Execute();
var saveresult = result.Id;
Google.Apis.Drive.v2.Data.File body = new Google.Apis.Drive.v2.Data.File();
body.Title = fileName;
body.Description = "A test document";
body.MimeType = "application/zip";
body.Parents = new List<ParentReference>() { new ParentReference() { Id = saveresult } };
var stream = new System.IO.MemoryStream(bytes);
FilesResource.InsertMediaUpload request = service.Files.Insert(body, stream, "text/plain");
request.Upload();
if (request.ResponseBody == null)
{
throw new Exception("User remove access to aplication.");
}
}
}
现在问题是在我尝试再次保存文件后保存文件一次它创建另一个文件夹并将文件保存在该文件夹中但我想检查文件夹是否存在而不是将文件保存在该文件夹中如果该文件夹不存在,则首先创建一个文件夹而不是保存该文件。 感谢。