c# - PowerBI API | Import PBIX -
i have method upload .pbix file powerbi app, method return id expected. reports , datasets doesn't appear on powerbi
my method :
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); (int = 0; < 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; }
can me ?
thanks
david almas pt-pt
if .../imports/{import id received}, return ids dataset , report once finished being created.
Comments
Post a Comment