WebAPI-文件下载校验和? [英] WebAPI - File download checksum?

查看:47
本文介绍了WebAPI-文件下载校验和?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我当前正在使用C#RestClient从Web API下载文件.这是我当前用于从Web API部分返回文件的代码:

I'm currently downloading a file from my Web API using a C# RestClient. This is my current code for returning a file from the Web API part:

[HttpGet]
public HttpResponseMessage Generate()
{
var stream = new MemoryStream();
// processing the stream.

var result = new HttpResponseMessage(HttpStatusCode.OK)
{
    Content = new ByteArrayContent(stream.GetBuffer())
};
result.Content.Headers.ContentDisposition =
    new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment")
{
    FileName = "CertificationCard.pdf"
};
result.Content.Headers.ContentType =
    new MediaTypeHeaderValue("application/octet-stream");

return result;
}

来自此:如何返回文件(FileContentResult)在ASP.NET WebAPI中

然后我的问题是,如何验证文件是否已正确下载-我可以以某种方式在ByteArray上提供MD5校验和并在RestClient中进行检查吗?还是完全不需要这样做?

My question is then, how can i validate that the file is downloaded correctly - can i somehow provide an MD5 checksum on the ByteArray and check this in the RestClient, or is this complete unnecessary?

推荐答案

您将生成文件的哈希,将其添加为响应标头,并验证客户端中的下载何时完成.

You would generate a hash of the file, add it as a response header and verify when the download completes within the client.

仅当您认为流或网络问题中可能存在TCP纠错处理能力之外的数据损坏机会时,这才有意义.

This would only make sense if you think there is a chance of corruption of the data within your stream or network issues outside the ability of TCP error correction to handle.

这是一个判断调用的必要性,请参见为什么在下载文件时比较校验和是一个好习惯?讨论.(考虑到散列&数据源自同一响应中的同一位置,那么安全性考虑实际上并不适用)

How necessary this is is a judgement call, see Why is it good practice to compare checksums when downloading a file? for a discussion. (Considering the hash & data originate from the same place in the same response, the security considerations don't really apply)

这篇关于WebAPI-文件下载校验和?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆