0

I am using tableau server for reporting in my application for reporting and I have created report files at tableau server, which is working fine. I have requirement to send these report file over the mail to user. I have exported report into byte [] using following code

var client = new RestSharp.RestClient(repartPath);
var request = new RestRequest(Method.GET);
request.AddHeader("cache-control", "no-cache");
request.AddHeader("Connection", "keep-alive");
request.AddHeader("Accept-Encoding", "gzip, deflate");
request.AddHeader("Host", hostname);
request.AddHeader("Postman-Token", tokenValue);
request.AddHeader("Cache-Control", "no-cache");
request.AddHeader("Accept", "*/*");
request.AddHeader("User-Agent", "PostmanRuntime/7.19.0");
request.AddHeader("X-Tableau-Auth", tableauToken);
IRestResponse response = client.Execute(request);
if (response.IsSuccessful)
{
 return response.RawBytes;
}

Above code is returning byte []. Now I want to covert byte [] into data table, to convert byte [] into data table.I am using below code:

using (MemoryStream stream = new MemoryStream(byteArrayData))
{
  BinaryFormatter binaryFormatter = new BinaryFormatter();
   _dataTable= (DataTable)binaryFormatter.Deserialize(stream);
}

But it is throws exception: input string not in valid binary format. Any help will be appreciated, many thanks in advance .

amit sijaria
  • 23
  • 1
  • 4

1 Answers1

0

When serializing data, the serialization and de-serialization methods must match. You show some code that makes a request, but it does not include the code that actually does the serialization. So the most probable answer that BinaryFormatter was not used to serialize the data.

However, binaryFormatter is probably not the right choice of serializer. This more or less takes how the data is represented in memory, and puts it down in a data stream. Notably, it might break just by changing .Net version, but there are many other problems with it.

If if the byte-array represents a file, you should convert the byte array into a stream and give the stream to whatever library can read the file. Or, if that is not possible, save the byte-array to a temporary file, and ask the library to open the file.

If you want to do the serialization yourself you would typically create a new type that is amiable for serialization, convert the data to that type, and use some serialization library to serialize it. Json.Net and Protobuf.Net are fairly popular serialization libraries but there are many more.

JonasH
  • 28,608
  • 2
  • 10
  • 23