使用PowerBI Rest API上传PBIX报表到指定的工作区

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_23502409/article/details/89845613
     public async Task<object> postImportReportToGroups(HttpPostedFileBase postedFileBase, string groupID)
        {
            var obj = new object();
            try
            {
                string fileName = postedFileBase.FileName;
                //var nameConflict = "Overwrite";
                //byte[] bytes = File.ReadAllBytes(@"G://Stock_In_Out_Report.pbix");
                var credential = new UserPasswordCredential(Username, Password);
                // Authenticate using created credentials
                var authenticationContext = new AuthenticationContext(AuthorityUrl);
                var authenticationResult = await authenticationContext.AcquireTokenAsync(ResourceUrl, ApplicationId, credential);
                var tokenCredentials = new TokenCredentials(authenticationResult.AccessToken, "Bearer");
                using (var client = new PowerBIClient(new Uri(ApiUrl), tokenCredentials))
                {
                    // 打开文件
                    //FileStream fileStream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read);
                    // 读取文件的 byte[]
                    byte[] bytes = new byte[postedFileBase.InputStream.Length];
                    postedFileBase.InputStream.Read(bytes, 0, bytes.Length);
                    postedFileBase.InputStream.Close();
                    //fileStream.Read(bytes, 0, bytes.Length);
                    //fileStream.Close();
                    // 把 byte[] 转换成 Stream
                    Stream stream = new MemoryStream(bytes);
                    var uploadResult = client.Imports.PostImportWithFileInGroup(groupID, stream, fileName, null); //上传报表
                    var getImportInfo = client.Imports.GetImportById(uploadResult.Id);//根据导入成功后得到的结果ID去获取上一步上传成功的报表信息  

                }
            }
            catch (Exception ex)
            {
                obj = new
                {
                    code = 0,
                    reportID = string.Empty
                };
                log.Error(ex);
            }
            return obj;
        }

猜你喜欢

转载自blog.csdn.net/qq_23502409/article/details/89845613