Exemplo n.º 1
0
        public async Task <string> ExemploGoogle(string _fileName, string _path, ModelGoogleDrive _googleDrive, string _folderId)
        {
            try
            {
                string postUri       = "https://www.googleapis.com/upload/drive/v3/files";
                var    responseToken = await CallManager.GetToken(_googleDrive);

                string _bearerToken = responseToken.Substring(17, 219);

                client.DefaultRequestHeaders.Add("Authorization", "Bearer " + _bearerToken);

                var request = new HttpRequestMessage()
                {
                    Method     = HttpMethod.Post,
                    RequestUri = new Uri(postUri)
                };

                request.Properties.Add("uploadType", "resumable");

                //var fileToUpload = new Google.Apis.Drive.v3.Data.File()
                //{
                //    Name = _fileName,
                //    MimeType = MimeTypesMap.GetMimeType(System.IO.Path.GetExtension(_path)),
                //    Parents = new List<string>
                //        {
                //            _folderId
                //        }
                //};


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

                var           length = stream.Length.ToString();
                StreamContent sc     = new StreamContent(stream);
                //sc.Headers.Add("Content-Type", "application/octet-stream");
                //sc.Headers.Add("Content-Length", length);

                request.Content = sc;
                var streamResult = await client.SendAsync(request);

                //var response = client.PostAsync(postUri, sc);
                return(await streamResult.Content.ReadAsStringAsync());
            }
            catch (Exception err)
            {
                throw err;
            }
            finally
            {
                //client.Dispose();
            }
        }
Exemplo n.º 2
0
        public static async Task <string> Upi(string _fileName, string _path, ModelGoogleDrive _googleDrive, string _folderId)
        {
            try
            {
                // 2. Create the url
                string postUri = "https://www.googleapis.com/drive/v3/files?uploadType=media";
                //string filename = "myFile.png";
                var response = await CallManager.GetToken(_googleDrive);

                string _bearerToken = response.Substring(17, 219);
                // In my case this is the JSON that will be returned from the post
                string result = "";

                using (var formContent = new MultipartFormDataContent("NKdKd9Yk"))
                {
                    formContent.Headers.ContentType.MediaType = "multipart/form-data";
                    // 3. Add the filename C:\\... + fileName is the path your file
                    Stream fileStream = System.IO.File.OpenRead(_path + _fileName);
                    formContent.Add(new StreamContent(fileStream), _fileName, _fileName);

                    using (var client = new HttpClient())
                    {
                        // Bearer Token header if needed
                        client.DefaultRequestHeaders.Add("Authorization", "Bearer " + _bearerToken);
                        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("multipart/form-data"));

                        try
                        {
                            // 4.. Execute the MultipartPostMethod
                            var message = await client.PostAsync(postUri, formContent);

                            // 5.a Receive the response
                            result = await message.Content.ReadAsStringAsync();
                        }
                        catch (Exception ex)
                        {
                            // Do what you want if it fails.
                            throw ex;
                        }
                    }
                }


                return(result);
            }
            catch (Exception ex)
            {
                return(ex.Message);
            }
        }
Exemplo n.º 3
0
        public static async Task <string> TesteUp(string _fileName, string _path, ModelGoogleDrive _googleDrive, string _folderId)
        {
            try
            {
                string postUri       = "https://www.googleapis.com/upload/drive/v3/files?uploadType=media";
                var    responseToken = await CallManager.GetToken(_googleDrive);

                string _bearerToken = responseToken.Substring(17, 219);

                var request = new HttpRequestMessage()
                {
                    Method     = HttpMethod.Post,
                    RequestUri = new Uri(postUri)
                };

                var path = _path + _fileName;

                if (System.IO.File.Exists(path))
                {
                    bool r = true;
                }

                using (var filestream = System.IO.File.OpenRead(path))
                {
                    var length        = filestream.Length.ToString();
                    var streamContent = new StreamContent(filestream);
                    streamContent.Headers.Add("Content-Type", "application/octet-stream");
                    streamContent.Headers.Add("Content-Length", length);


                    request.Content = streamContent;

                    var response = new HttpClient().SendAsync(request).Result;
                }

                return(" ");
            }
            catch (Exception err)
            {
                throw err;
            }
        }
Exemplo n.º 4
0
        public static async Task <string> Upis(string _fileName, string _path, ModelGoogleDrive _googleDrive, string _folderId)
        {
            try
            {
                var responseToken = await CallManager.GetToken(_googleDrive);

                string _bearerToken = responseToken.Substring(17, 219);
                var    client       = new RestClient("https://www.googleapis.com/upload/drive/v3/files?uploadType=media");
                client.Timeout = -1;
                var request = new RestRequest(Method.POST);
                request.AddParameter("Authorization", "Bearer " + _bearerToken);
                request.AddHeader("Content-Type", "multipart/form-data");
                request.AddHeader("Content-Length", "436490240");
                request.AddFile("CeltaBSEmporioSaborBackupFull.bak", @"C:\Temp\backup\CeltaBSEmporioSaborBackupFull.bak");
                IRestResponse response = client.Execute(request);
                return(response.Content);
            }
            catch (Exception err)
            {
                return(err.Message);
            }
        }
Exemplo n.º 5
0
        public static async Task <string> Up(string _fileName, string _path, ModelGoogleDrive _googleDrive, string _folderId)
        {
            try
            {
                string postUri       = "https://www.googleapis.com/drive/v3/files?uploadType=resumable";
                var    responseToken = await CallManager.GetToken(_googleDrive);

                string _bearerToken = responseToken.Substring(17, 219);

                client.DefaultRequestHeaders.Add("Authorization", "Bearer " + _bearerToken);

                using (HttpRequestMessage requestMessage = new HttpRequestMessage()
                {
                    Method = HttpMethod.Post,
                    RequestUri = new Uri(postUri)
                })
                {
                    using (var stream = new System.IO.FileStream(_path + _fileName, System.IO.FileMode.Open, System.IO.FileAccess.Read))
                    {
                        var           length = stream.Length.ToString();
                        StreamContent sc     = new StreamContent(stream);
                        sc.Headers.Add("Content-Type", "application/json; charset=UTF-8");
                        sc.Headers.Add("Content-Length", length);

                        requestMessage.Content = sc;

                        var response = client.PostAsync(postUri, requestMessage.Content).Result;
                        return(response.Content.ToString());
                    }
                }
                //var request = new HttpRequestMessage()
                //{
                //    Method = HttpMethod.Post,
                //    RequestUri = new Uri(postUri)
                //};



                //using (var stream = new System.IO.FileStream(_path + _fileName, System.IO.FileMode.Open, System.IO.FileAccess.Read))
                //{
                //    var length = stream.Length.ToString();
                //    StreamContent sc = new StreamContent(stream);
                //    sc.Headers.Add("Content-Type", "application/octet-stream");
                //    sc.Headers.Add("Content-Length", length);

                //    request.Content = sc;

                //    var response = new HttpClient().SendAsync(request).Result;

                //    //using (var formData = new MultipartFormDataContent())
                //    //{
                //    //    formData.Add(sc);

                //    //    var response = client.PostAsync(postUri, formData).Result;

                //    //    return response.StatusCode.ToString();
                //    //}
                //    return await response.Content.ReadAsStringAsync();
                //}
            }
            catch (Exception err)
            {
                HelperLogs.WriteLog(err.Message + "\br" + err.StackTrace);
                return("ERRO: " + err.Message);
            }
        }