2017-05-25 113 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