如何将文件上传到asp.net中的google驱动器 [英] How to upload file into google drive in asp.net

查看:86
本文介绍了如何将文件上传到asp.net中的google驱动器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

// TO:DO

var fileName = System.IO.Path.GetFileName(file.FileName);

var path = System.IO.Path.Combine (Server.MapPath(〜/ Content / Upload),fileName);

file.SaveAs(path);

ModelState.Clear();

UserCredential凭证= GoogleWebAuthorizationBroker.AuthorizeAsync(

新ClientSecrets

{

ClientId = ClientId,

ClientSecret = ClientSecret,

},

new [] {DriveService.Scope.Drive},

user,

CancellationToken.None).Result;



//创建服务。

var service = new Dri veService(new BaseClientService.Initializer()

{

HttpClientInitializer = credential,

ApplicationName = APIID,

} ); $









文件正文=新文件();

body.Title =Candidates Document-+ System.IO.Path.GetFileNameWithoutExtension(file.FileName);

body.Description =Carrer document;

body.MimeType =application / msword;



byte [] byteArray = System.IO.File.ReadAllBytes(path);

System.IO.MemoryStream stream = new System.IO.MemoryStream(byteArray);



FilesResource.InsertMediaUpload request = service.Files.Insert( body,stream,application / msword);

request.Upload();



文件driveFile = request.ResponseBody;



ViewBag.Message =文件已成功上传。文件ID:+ driveFile.Id;



此代码运行良好。但我不认为我是管理员用户我有谷歌驱动器。我的网站有很多用户他们上传我的驱动器上的文件没有任何认证

请尽快回复。

//TO:DO
var fileName = System.IO.Path.GetFileName(file.FileName);
var path = System.IO.Path.Combine(Server.MapPath("~/Content/Upload"), fileName);
file.SaveAs(path);
ModelState.Clear();
UserCredential credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
new ClientSecrets
{
ClientId = ClientId,
ClientSecret = ClientSecret,
},
new[] { DriveService.Scope.Drive },
"user",
CancellationToken.None).Result;

// Create the service.
var service = new DriveService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = APIID,
});




File body = new File();
body.Title = "Candidates Document-" + System.IO.Path.GetFileNameWithoutExtension(file.FileName);
body.Description = "Carrer document";
body.MimeType = "application/msword";

byte[] byteArray = System.IO.File.ReadAllBytes(path);
System.IO.MemoryStream stream = new System.IO.MemoryStream(byteArray);

FilesResource.InsertMediaUpload request = service.Files.Insert(body, stream, "application/msword");
request.Upload();

File driveFile = request.ResponseBody;

ViewBag.Message = "File uploaded successfully.File id: " + driveFile.Id;

This code working good. But I wont that I am admin User I have google drive .There are many user of my site they upload there document on my drive without any authentication
Please reply as soon as possible .

推荐答案

查看此 https://developers.google.com/drive/的在线帮助 [ ^ ]


这篇关于如何将文件上传到asp.net中的google驱动器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆