我有一种将.PBIX文件上传到我的PowerBI App的方法,此方法按预期返回一个id。但是,报告和数据集并未出现在PowerBI
上我的方法:
public static string SendFile()
{
string[] files = { "D://David/Documents//PBIX_TEST.pbix" };
string boundary = "----------------------------" + DateTime.Now.Ticks.ToString("x");
HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create("https://api.powerbi.com/beta/myorg/imports?datasetDisplayName=PBI_TESTT");
httpWebRequest.ContentType = "multipart/form-data; boundary=" + boundary;
httpWebRequest.Method = "POST";
httpWebRequest.KeepAlive = true;
//httpWebRequest./*Credentials*/ = System.Net.CredentialCache.DefaultCredentials;
httpWebRequest.Headers.Add("Authorization", String.Format("Bearer {0}", nsToken.TokenSingleton.Instance.token.AccessToken));
Stream memStream = new System.IO.MemoryStream();
byte[] boundarybytes = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n");
string formdataTemplate = "\r\n--" + boundary + "\r\nContent-Disposition: form-data; name=\"{0}\";\r\n\r\n{1}";
string headerTemplate = "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\n Content-Type: application/octet-stream\r\n\r\n";
memStream.Write(boundarybytes, 0, boundarybytes.Length);
for (int i = 0; i < files.Length; i++)
{
string header = string.Format(headerTemplate, "fieldNameHere", "PBIX_TEST.pbix");
//string header = string.Format(headerTemplate, "uplTheFile", files[i]);
byte[] headerbytes = System.Text.Encoding.UTF8.GetBytes(header);
memStream.Write(headerbytes, 0, headerbytes.Length);
FileStream fileStream = new FileStream(files[i], FileMode.Open,
FileAccess.Read);
byte[] buffer = new byte[1024];
int bytesRead = 0;
while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
{
memStream.Write(buffer, 0, bytesRead);
}
memStream.Write(boundarybytes, 0, boundarybytes.Length);
fileStream.Close();
}
httpWebRequest.ContentLength = memStream.Length;
Stream requestStream = httpWebRequest.GetRequestStream();
memStream.Position = 0;
byte[] tempBuffer = new byte[memStream.Length];
memStream.Read(tempBuffer, 0, tempBuffer.Length);
memStream.Close();
requestStream.Write(tempBuffer, 0, tempBuffer.Length);
requestStream.Close();
try
{
WebResponse webResponse = httpWebRequest.GetResponse();
Stream stream = webResponse.GetResponseStream();
StreamReader reader = new StreamReader(stream);
string var = reader.ReadToEnd();
var jsonSerializer = new JavaScriptSerializer();
var dataset = (dataset)jsonSerializer.Deserialize(var, typeof(dataset));
return dataset.Id;
}
catch (Exception ex)
{
throw ex;
}
httpWebRequest = null;
}
任何人都可以帮助我吗?
由于
大卫·阿尔马斯 PT-PT
答案 0 :(得分:0)
如果您执行GET ... / imports / {您收到的导入ID},它将返回数据集的ID并在完成创建后报告。
答案 1 :(得分:0)
我认为你的解决方案是正确的。我在编写解决方案时使用了它,它使用了HttpClient方法。
API documentation并不是非常具体,但是我需要dataSetDisplayName查询字符串参数,我知道你已经这样做了。
另外,API文档没有提及,但此处支持组。这是我的代码,它按预期工作。
public async Task<UploadFileResponse> UploadImportAsync(string displayName, Guid? groupId = null, ConflictInstruction conflictInstruction = ConflictInstruction.None)
{
if (string.IsNullOrEmpty(displayName))
throw new ArgumentNullException(nameof(displayName), "Display name cannot be null");
await AcquireTokenAsync();
string url = "https://api.powerbi.com/beta/myorg/imports/?dataSetDisplayName=" + HttpUtility.UrlEncode(displayName);
if (groupId.HasValue)
{
url = string.Concat("https://api.powerbi.com/beta/myorg/groups/", groupId, "/imports/?datasetDisplayName=", displayName);
}
if (conflictInstruction != ConflictInstruction.None)
{
url += "&nameConflict=" + conflictInstruction;
}
//just a test file
byte[] bytes = File.ReadAllBytes(@"C:\Users\chris\Documents\Power BI\FarmData.pbix");
string fileName = "Myfile.pbix";
var result = await PerformFileUploadAsync(url, bytes, fileName);
UploadFileResponse uploadFileResponse = JsonConvert.DeserializeObject<UploadFileResponse>(result, serializerSettings);
return uploadFileResponse;
}
然后是过程的主要内容:
private async Task<string> PerformFileUploadAsync(string url, byte[] bytes, string fileName)
{
using (HttpClient client = new HttpClient())
{
using (HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, url))
{
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", authenticationResult.AccessToken);
request.Headers.Add("Keep-Alive", "true");
request.Headers.Accept.Add(MediaTypeWithQualityHeaderValue.Parse("application/json"));
string boundary = string.Concat("---------------------------", DateTime.Now.Ticks.ToString("x", CultureInfo.InvariantCulture));
MultipartFormDataContent requestContent = new MultipartFormDataContent(boundary);
ByteArrayContent byteArrayContent = new ByteArrayContent(bytes);
byteArrayContent.Headers.ContentType = MediaTypeHeaderValue.Parse("application/x-zip-compressed");
byteArrayContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data") { Name = "file0", FileName = fileName };
requestContent.Add(byteArrayContent);
request.Content = requestContent;
try
{
HttpResponseMessage response = await client.SendAsync(request);
var responseData = await response.Content.ReadAsStringAsync();
if (response.IsSuccessStatusCode)
return responseData;
StringBuilder sb = new StringBuilder();
sb.AppendLine("Url:" + url);
sb.AppendLine("Method: " + HttpMethod.Post);
sb.AppendLine("HttpStatusCode: " + (int)response.StatusCode);
sb.AppendLine("Unable to retrieve data from service: " + responseData);
Log.Error(sb.ToString());
throw new HttpException((int)response.StatusCode, responseData);
}
catch (Exception ex)
{
Console.Write(ex.Message);
throw;
}
}
}
}