/
Form1.cs
116 lines (99 loc) · 4.31 KB
/
Form1.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
using System;
using System.Windows.Forms;
using Google.Apis.Services;
using Google.Apis.Auth.OAuth2;
using Google.Apis.Drive.v3;
using Google.Apis.Util.Store;
using System.Threading;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
DialogResult result = openFileDialog1.ShowDialog();
if(result == DialogResult.OK)
{
textBox1.Text = openFileDialog1.FileName;
}
}
private void btn_Upload_Click(object sender, EventArgs e)
{
Authorize();
}
private void Authorize()
{
string[] scopes = new string[] { DriveService.Scope.Drive,
DriveService.Scope.DriveFile,};
var clientId = "136899674016-mef1nqh4tl80rqg5ea2v7idur4f3md5h.apps.googleusercontent.com";
var clientSecret = "gdBaftWOdpHcn7paAiejttjm";
var credential = GoogleWebAuthorizationBroker.AuthorizeAsync(new ClientSecrets
{
ClientId = clientId,
ClientSecret = clientSecret
}, scopes,
Environment.UserName, CancellationToken.None, new FileDataStore("MyAppsToken")).Result;
DriveService service = new DriveService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = "UploadFile",
});
service.HttpClient.Timeout = TimeSpan.FromMinutes(100);
var response = uploadFile(service, textBox1.Text, "");
MessageBox.Show("Process completed--- Response--" + response);
}
public Google.Apis.Drive.v3.Data.File uploadFile(DriveService _service, string _uploadFile, string _parent, string _descrp = "Uploaded with .NET!")
{
if (System.IO.File.Exists(_uploadFile))
{
Google.Apis.Drive.v3.Data.File body = new Google.Apis.Drive.v3.Data.File();
body.Name = System.IO.Path.GetFileName(_uploadFile);
body.Description = _descrp;
body.MimeType = GetMimeType(_uploadFile);
byte[] byteArray = System.IO.File.ReadAllBytes(_uploadFile);
System.IO.MemoryStream stream = new System.IO.MemoryStream(byteArray);
try
{
FilesResource.CreateMediaUpload request = _service.Files.Create(body, stream, GetMimeType(_uploadFile));
request.SupportsTeamDrives = true;
request.ProgressChanged += Request_ProgressChanged;
request.ResponseReceived += Request_ResponseReceived;
request.Upload();
return request.ResponseBody;
}
catch (Exception e)
{
MessageBox.Show(e.Message, "Error Occured");
return null;
}
}
else
{
MessageBox.Show("The file does not exist.", "404");
return null;
}
}
private static string GetMimeType(string fileName)
{
string mimeType = "application/unknown";
string ext = System.IO.Path.GetExtension(fileName).ToLower();
Microsoft.Win32.RegistryKey regKey = Microsoft.Win32.Registry.ClassesRoot.OpenSubKey(ext);
if (regKey != null && regKey.GetValue("Content Type") != null) mimeType = regKey.GetValue("Content Type").ToString(); System.Diagnostics.Debug.WriteLine(mimeType); return mimeType;
}
private void Request_ProgressChanged(Google.Apis.Upload.IUploadProgress obj)
{
// textBox2.Text += obj.Status + " " + obj.BytesSent;
}
private void Request_ResponseReceived(Google.Apis.Drive.v3.Data.File obj)
{
if (obj != null)
{
MessageBox.Show("File was uploaded sucessfully--" );
}
}
}
}