1

I've created a SomeFile class:

C#:

public class SomeFile
{
    public byte[] Content { get; set; }

    public string MimeType { get; set; }

    public string Name { get; set; }
}

and this file is returned in a such way:

public async Task<IActionResult> GetFiles(string guid)
{            
    return Ok(new SomeFile() { Content = zippedFiles.ToArray(), 
                   Name = $"zippedFiles.zip", MimeType = "application/x-zip-compressed" });
}

At Angular side I've created model file:

Angular:

export interface SomeFile {    
    Content: **Uint8Array** //am I correct to use this type?
    MimeType: string
    Name: string
}

and http service gets this object like this:

public downloadZip(): Observable<any> {
    return this.http 
        .get(fooUrl)
        .map((response: Response) => <SomeFile> response.json());  
};

What type should I use for Content property at Angular side? Am I correct to use Uint8Array? Cause I get the error:

ERROR SyntaxError: Unexpected token P in JSON at position 0

May be I should not do .map((response: Response) => <SomeFile> response.json());?

5
  • You can't return binary data in json. You must encode it in base64 before you put it into JSON. Commented May 25, 2017 at 12:01
  • @Tseng do you mean? System.Convert.ToBase64String(image) Commented May 25, 2017 at 12:02
  • @Tseng and what type should I use for Content property at Angular side? Commented May 25, 2017 at 12:03
  • 2
    Yes and you have to treat it as string (both server and client sided) Commented May 25, 2017 at 12:04
  • @Tseng please, make a reply and I'll mark it as asnwer. Commented May 25, 2017 at 12:34

1 Answer 1

1

JSON Format can't treat binary files, so you need to encode the binary information into base64 string and then return it.

As a result, you have to change the type from byte[] to string.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.