从ASP.NET Core收到的实体客户端模型中的Parse字节数组

时间:2017-05-25 11:57:18

标签: c# angular typescript asp.net-core

我创建了一个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" });
}

Angular方面,我创建了模型文件

角:

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());  
};

我应该使用什么类型的 Angular端的内容属性? 我使用Uint8Array是否正确? 因为我收到错误:

  

ERROR SyntaxError:位于0的JSON中的意外标记P

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

1 个答案:

答案 0 :(得分:1)

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

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