簡體   English   中英

字節數組與C#中的Web Api方法不同嗎?

[英]Byte array different from an Web Api Method in C#?

所以我有一個Web API,它返回像這樣的字節數組

public byte[] Validate()
{
    byte[] buffer = licensePackage.ToByteArray();
    return buffer;
}

問題是,當我在客戶端上獲得它時,它的大小和字節數組不同,我在Google上搜索了一下,發現此鏈接很有幫助http://www.codeproject.com/Questions/778483/How-to-Get-byte-array-properly從Web Api-Met

但是我知道為什么會這樣嗎? 另外,從服務器發回該文件內容的另一種方法是什么?

對於給定的信息,我認為它必須與內容協商有關。 我不能說出原因,但是我確定這是在Web Api后提供文件服務的另一種方法。

var response = Request.CreateResponse(HttpStatusCode.OK);
var stream = new FileStream(pathToFile, FileMode.Open, FileAccess.Read);

response.Content = new StreamContent(stream);
response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
return ResponseMessage(response);

使用此解決方案,您可以提供返回IHttpActionResult實例的文件內容。 並且,返回帶有StreamContent的響應,您將返回Web Api內容協商不得修改的流。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM