C# 如何使用GoogleDrive API和C将文件上载到GoogleDrive#
我想使用Google Drive API从我的ASP.NET项目上载文件 如何在ASP.NET中使用C# 如何使用GoogleDrive API和C将文件上载到GoogleDrive#,c#,asp.net,google-drive-api,C#,Asp.net,Google Drive Api,我想使用Google Drive API从我的ASP.NET项目上载文件 如何在ASP.NET中使用DriveService 我搜索了很多,但没有得到有关DriveService的正确结果,请尝试 和来自:- 使用Google.api.Drive.v2; 使用Google.api.Drive.v2.Data; 使用Google.api.Requests; 使用System.Collections.Generic; Net系统; // ... 公共类MyClass{ // ... /// ///
DriveService
我搜索了很多,但没有得到有关DriveService
的正确结果,请尝试
和来自:-
使用Google.api.Drive.v2;
使用Google.api.Drive.v2.Data;
使用Google.api.Requests;
使用System.Collections.Generic;
Net系统;
// ...
公共类MyClass{
// ...
///
///插入新文件。
///
///驱动API服务实例。
///要插入的文件的标题,包括扩展名。
///要插入的文件的说明。
///父文件夹的ID。
///要插入的文件的MIME类型。
///要插入的文件的文件名。
///插入的文件元数据,如果发生API错误,则返回null。
私有静态文件insertFile(DriveService服务、字符串标题、字符串描述、字符串parentId、字符串mimeType、字符串文件名){
//文件的元数据。
文件体=新文件();
正文.标题=标题;
body.Description=描述;
body.MimeType=MimeType;
//设置父文件夹。
如果(!String.IsNullOrEmpty(parentId)){
body.Parents=新列表()
{new ParentReference(){Id=parentId}};
}
//文件的内容。
byte[]byteArray=System.IO.File.ReadAllBytes(文件名);
MemoryStream stream=新的MemoryStream(byteArray);
试一试{
fileresource.InsertMediaUpload请求=service.Files.Insert(body、stream、mimeType);
request.Upload();
File File=request.ResponseBody;
//取消注释以下行以打印文件ID。
//Console.WriteLine(“文件ID:+File.ID”);
返回文件;
}捕获(例外e){
Console.WriteLine(“发生错误:+e.Message”);
返回null;
}
}
very emergency==red rag to bull==雇佣某人。欢迎来到Stack Overflow。在发布问题之前,请尝试在研究和编码方面展示自己的努力。感谢您的帮助,我的主要问题是创建DriveService实例,如何创建DriveService实例请帮助我如果您单击我粘贴的链接,您会发现“var service=new DriveService(new BaseClientService.Initializer()”
using Google.Apis.Drive.v2;
using Google.Apis.Drive.v2.Data;
using Google.Apis.Requests;
using System.Collections.Generic;
using System.Net;
// ...
public class MyClass {
// ...
/// <summary>
/// Insert new file.
/// </summary>
/// <param name="service">Drive API service instance.</param>
/// <param name="title">Title of the file to insert, including the extension.</param>
/// <param name="description">Description of the file to insert.</param>
/// <param name="parentId">Parent folder's ID.</param>
/// <param name="mimeType">MIME type of the file to insert.</param>
/// <param name="filename">Filename of the file to insert.</param>
/// <returns>Inserted file metadata, null is returned if an API error occurred.</returns>
private static File insertFile(DriveService service, String title, String description, String parentId, String mimeType, String filename) {
// File's metadata.
File body = new File();
body.Title = title;
body.Description = description;
body.MimeType = mimeType;
// Set the parent folder.
if (!String.IsNullOrEmpty(parentId)) {
body.Parents = new List<ParentReference>()
{new ParentReference() {Id = parentId}};
}
// File's content.
byte[] byteArray = System.IO.File.ReadAllBytes(filename);
MemoryStream stream = new MemoryStream(byteArray);
try {
FilesResource.InsertMediaUpload request = service.Files.Insert(body, stream, mimeType);
request.Upload();
File file = request.ResponseBody;
// Uncomment the following line to print the File ID.
// Console.WriteLine("File ID: " + file.Id);
return file;
} catch (Exception e) {
Console.WriteLine("An error occurred: " + e.Message);
return null;
}
}