2017-05-25 115 views
0

我创建了一个SomeFile类:解析字节数组核心

C#:

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

    public string MimeType { get; set; } 

    public string Name { get; set; } 
} 

且该文件是在这样的方式返回:

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

角边,我创建的模型文件

角:

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

和HTTP服务获取该对象是这样的:

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

我应该使用内容酒店在角侧什么类型的?我正确的使用Uint8Array? 因为我得到的错误:

ERROR SyntaxError: Unexpected token P in JSON at position 0

可能是我不应该做.map((response: Response) => <SomeFile> response.json());

+0

你不能在json中返回二进制数据。在将它放入JSON之前,您必须使用base64进行编码。 – Tseng

+0

@Tseng你的意思是? 'System.Convert.ToBase64String(image)' – StepUp

+0

@Tseng以及我应该在Angular的'Content'属性中使用什么类型? – StepUp

回答

1

JSON格式无法处理二进制文件,因此您需要将二进制信息编码为base64字符串,然后将其返回。

因此,您必须将类型从byte[]更改为string