0

I have an object T. I need to send the file representation of it into a web service. Without saving the file to a temp file.

WebService method:

 myClient.SendFile(
    new SendFileData{ 
        id = "1",
        fileName = "Foo",
        fileType = "json",
        fileContent = base64, // string Base64 variable here
    }

To get the Base64 of a file I use :

public static string FileToBase64(string path) => FileToBase64(File.ReadAllBytes(path));
public static string FileToBase64(Byte[] bytes) => Convert.ToBase64String(bytes);

I have made those method to work on a temp file stored in a directory. saving the Json to file using :

using (StreamWriter file = File.CreateText(tempPath))
{
    JsonSerializer serializer = new JsonSerializer();
    serializer.Serialize(file, _data);
}

And reading it like:

  Directory.GetFiles(directory).Where().Select(x=> new {
                    fi = new FileInfo(f),
                    name = Path.GetFileNameWithoutExtension(f),
                    ext = Path.GetExtension(f).Trim('.'),
                    content = FileBase64Helper.FileToBase64(f)
                })

I try to make a in memory file and convert it to b64 like:

public void Demo()
{
    var myObject = new CustomType { Id = 1, Label = "Initialisation of a custom object" };
    var stringRepresentation = 
 JsonConvert.SerializeObject(myObject, Formatting.Indented, new JsonSerializerSettings { });

    SendFileData dataProjection = new SendFileData { };
    var result = FakeClient.SendFile(dataProjection);
}
public class CustomType
{
    public string Label { get; set; }
    public int Id { get; set; }
}
public static class FakeClient
{
    public static bool SendFile(SendFileData data) => true;
}
public class SendFileData
{
    public string id { get; set; }
    public string fileName { get; set; }
    public string fileType { get; set; }
    public string fileContent { get; set; }
}


Comparaison between direct convertion and FileReadByte:
var myObject = new CustomType { Id = 1, Label = "Initialisation of a custom object" };
var stringRepresentation = JsonConvert.SerializeObject(myObject, Formatting.Indented, new JsonSerializerSettings { });

var directSerialization = Convert.ToBase64String(Encoding.UTF8.GetBytes(stringRepresentation));

var tempPath = @"test.json";
using (StreamWriter file = File.CreateText(tempPath))
{
    JsonSerializer serializer = new JsonSerializer();
    serializer.Serialize(file, myObject);
}

var fromFile = FileBase64Helper.FileToBase64(tempPath);

SendFileData dataProjection = new SendFileData { };
var result = FakeClient.SendFile(dataProjection);

Direct serialization:

ew0KICAiTGFiZWwiOiAiSW5pdGlhbGlzYXRpb24gb2YgYSBjdXN0b20gb2JqZWN0IiwNCiAgIklkIjogMQ0KfQ==

From file

eyJMYWJlbCI6IkluaXRpYWxpc2F0aW9uIG9mIGEgY3VzdG9tIG9iamVjdCIsIklkIjoxfQ==

xdtTransform
  • 1,986
  • 14
  • 34
  • How do you receive the file initially? Presuming at some point it's a `byte[]` in that case you should be able to just stream this without saving the file. – Liam Jan 21 '19 at 13:16
  • @Liam, I start from an object. And have to end with a Base64 string that is the file. – xdtTransform Jan 21 '19 at 13:21
  • @Liam, I thought that the base64 read from a file will be diffenrent from the base64 from direct convertion. Are they the same? If they are the project from the object will be simple – xdtTransform Jan 21 '19 at 13:22
  • If the file is a text file, then File.ReadAllBytes will read the content of the text file. In this case reading from a file will be the same as the base64 from direct conversion. Could you elaborate more on what's not working yet? – Lennart Jan 21 '19 at 13:37
  • @lenn , Just tested and they are not the same. Let me edit that into the question. I must be missing something simple. – xdtTransform Jan 21 '19 at 13:39

1 Answers1

0

If you're question is as follows and if i understand it correctly:

"Does File.ReadAllBytes add additional information about the file in it's return value, or is the return value equal to just having the content of the file as a byte array?"

Answer:

According to Microsoft Documentation, it will return the content of the file as a byte array.

File.ReadAllBytes

Hope this helps!

Source: https://learn.microsoft.com/en-us/dotnet/api/system.io.file.readallbytes?view=netframework-4.7.2

EDIT: Decoding the base64, i found this: The difference in the base64 encoded values is the json formatting, nothing else :D

Direct serialization:

first

From file:

second

Lennart
  • 752
  • 1
  • 5
  • 18