Exemplo n.º 1
0
        public async Task <bool> DownloadFile <T>(T t, FileInformation source, string targetPath, string taskId)
        {
            string token = t as string;

            if (!Directory.Exists(targetPath))
            {
                Directory.CreateDirectory(targetPath);
            }
            string savePath    = Path.Combine(targetPath, source.FileName);
            string downLoadUrl = "";

            try
            {
                string host = "graph.microsoft.com";
                string path = "/v1.0/me/drive/items/";
                path = Path.Combine(path, source.FileId);
                NetClient netClient = new NetClient();
                netClient.AddHeader("SampleID", "uwp-csharp-connect-sample");
                netClient.AddHeader("SdkVersion", "Graph-dotnet-1.6.2");
                netClient.AddHeader("Authorization", string.Format("Bearer {0}", token));
                string result = netClient.GET(host, path, true);
                IDictionary <string, Object> driveItem = JsonConvert.DeserializeObject <IDictionary <string, Object> >(result);
                Object obj = "";
                driveItem.TryGetValue("@microsoft.graph.downloadUrl", out obj);
                downLoadUrl = obj.ToString();

                LoadEntity loadEntity = new LoadEntity()
                {
                    Url       = downLoadUrl,
                    FileSize  = source.FileSize,
                    ChunkSize = 1024 * 1024,
                    TaskId    = taskId
                };

                NetUtil netUtil   = new NetUtil();
                bool    isSuccess = await netUtil.LoadAsync(loadEntity, savePath, null, ProgressEvent);

                return(isSuccess);
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
        }
        /// <summary>
        /// 获取下载路径
        /// </summary>
        /// <param name="parentId"></param>
        /// <param name="fileName"></param>
        /// <param name="token"></param>
        /// <returns></returns>
        private string GetUploadUrl(string parentId, string fileName, string token)
        {
            string uploadUrl = "";
            string host      = "graph.microsoft.com";
            string path      = null; //string.Format("/v1.0/me/drive/{0}:/{1}:/microsoft.graph.createUploadSession", parentId, fileName);

            if ("root" == parentId || string.IsNullOrEmpty(parentId))
            {
                path = string.Format("/v1.0/me/drive/{0}:/{1}:/microsoft.graph.createUploadSession", parentId,
                                     fileName);
            }
            else
            {
                path = string.Format("/v1.0/me/drive/items/{0}:/{1}:/microsoft.graph.createUploadSession", parentId,
                                     fileName);
            }
            NetClient netClient = new NetClient();

            try
            {
                netClient.AddHeader("SampleID", "uwp-csharp-connect-sample");
                netClient.AddHeader("SdkVersion", "Graph-dotnet-1.6.2");
                netClient.AddHeader("Cache-Control", "no-store, no-cache");
                netClient.AddHeader("Content-Type", "application/json");
                netClient.AddHeader("Host", "graph.microsoft.com");

                netClient.AddHeader("Authorization", string.Format("Bearer {0}", token));
                string data         = "{}";
                string uploadPreper = netClient.POST(data, host, path, false, true);
                IDictionary <string, object> dic = JsonConvert.DeserializeObject <Dictionary <string, object> >(uploadPreper);
                if (dic != null && dic.ContainsKey("uploadUrl"))
                {
                    uploadUrl = dic["uploadUrl"].ToString();
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
            return(uploadUrl);
        }