-3

My issue is when I create this controller :

[HttpPost("[Action]")]
public IActionResult CreateFile(string word){
    word = "test";

    byte[] b = Encoding.ASCII.GetBytes(word);

    var txtBuilder = new StringBuilder();
    txtBuilder.Append(b);
    var txtContent = txtBuilder.ToString();
    var txtStream = new MemoryStream(Encoding.UTF8.GetBytes(txtContent));

    return File(txtStream,"text/plain","license.dat");
}

When I try this request on swagger, it allow me to download a file. When I download the file the content is :

System.Byte[]

But when I test this on swagger :

[HttpPost("[Action]")]
public IActionResult CreateFile(string word){
    word = "test";

    byte[] b = Encoding.ASCII.GetBytes(word);

    return BadRequest(b);
}

the result of b is :

dGVzdA==

I don't want to see test or System.Byte[] inside the created file I want to see the result of b on the created file.

14
  • 2
    Could you clarify exactly what you expect to see in the file? It's not clear what you're trying to achieve, or why you're converting back and forth between binary and text. Commented Apr 25, 2019 at 8:26
  • Should check that "text/plain" Commented Apr 25, 2019 at 8:38
  • 1
    What exactly do you mean by "binary data"? Everything that goes across the wire is binary in the end. What exact bytes do you expect to be in that MemoryStream? 4 bytes, one for each character? Or some textual representation (e.g. in hex)? Commented Apr 25, 2019 at 8:41
  • @JonSkeet I update my ticket. Commented Apr 25, 2019 at 8:42
  • Your edit doesn't make sense to me - because the value of word is "test". What you've claimed to be the result is the base64-encoded version of the ASCII-encoded bytes of "test". Where are you seeing that result? Please edit your question to make it a lot clearer - while it's unclear, we're not going to be able to help you. Commented Apr 25, 2019 at 8:44

3 Answers 3

1

Ok, I've got you.

This is what you want:

[HttpGet("CreateFile")]
public IActionResult CreateFile(string word)
{
    word = "test";

    byte[] b = Encoding.ASCII.GetBytes(word);
    string base64 = System.Convert.ToBase64String(b);
    b = Encoding.ASCII.GetBytes(base64);
    var txtStream = new MemoryStream(b);

    return File(txtStream, "text/plain", "license.dat");
}
Sign up to request clarification or add additional context in comments.

2 Comments

Its just me or this has nothing to do with "I want to have bynary data in the file" yet it is useful to OP? The OP coments in the question were missleading
@bradbury9 yep, you are right. But it doesn't matter anymore. It's cleared already.
0

Check the following answer. You should have a right solution there: Returning CSV from .NET Core controller

TL;DR: Use FileResult instead of IActionResult or set the Content-Disposition header.

Another very good article: How to Return Files From Web API

Explanation about: System.Byte[]

I guess, since you've set the content-type to "text/plain" there is .ToString() convertion somewhere. Look at the first conneto to this question. "System.Byte[]" is being returned instead of the actual data

Array does not override this method, therefore for arrays it returns the type name - that's the default implementation.

2 Comments

I try but nothing work for me :/
@user10863293 check my answer -> stackoverflow.com/a/55847016/2487565.
0

I am not sure, what do you mean with the result "dGVzdA=="

But this simple works:

[HttpGet("CreateFile")]
public IActionResult CreateFile(string word)
{
    word = "test";

    byte[] b = Encoding.ASCII.GetBytes(word);
    var txtStream = new MemoryStream(b);

    return File(txtStream, "text/plain", "license.dat");
}

I am getting a file with "test" inside.

But you should better use

public FileResult CreateFile(string word)

And, Of cource, you can use UTF8 instead of ASCII

byte[] b = Encoding.UTF8.GetBytes(word);

4 Comments

Of cource, you can use UTF8 instead of ASCII
Yes But I don't want to have a file with test inside. When I do a Console.WriteLine of Encoding.ASCII.GetBytes(word) I have this result on my api dGVzdA== I also want to have the same result inside my file when it will be created.
@user10863293. What do you want to have instead of "test"? If you want to have text from the word parameter, then delete the line word = "test";
@user10863293: Then you need to clarify what you do want. It's really, really unclear what you want, how you're downloading the file etc.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.