来自HttpClient.GetStreamAsync的GetFile大小

时间:2019-02-06 13:13:53

标签: c# .net-core asp.net-core-mvc

在返回HttpClient之前,如何检索fileStreamResult携带的文件的大小

    public async Task<FileStreamResult> Index(string id)
    {
        var urlToDownloadFromService= _upDownService.Downloadables.Find(d => d.Name == id);

        HttpClient client = new HttpClient();
        Stream stream = await client.GetStreamAsync(new Uri(urlToDownloadFromService.UrlSource));
        var fileStreamResult =  new FileStreamResult(stream, "application/octet-stream");
        return fileStreamResult;
    }

stream.Length不起作用,因为ContentLengthReadStream未实现该方法

更新

在dotnetcore框架中,HttpConnection的实现像这样从response对象获取长度信息

else if (response.Content.Headers.ContentLength != null)
{
     long contentLength = Response.Content.Headers.ContentLength.GetValueOrDefault();
.
.
.
.

     responseStream = new ContentLengthReadStream(this, (ulong)contentLength);

Github source httpConnection

0 个答案:

没有答案