微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c# – ASPNET核心返回文件使用IE11失败? Content_Length不匹配

我有一个返回文件的控制器方法,并且在所有浏览器中,这与IE11不同.在IE11中,我得到500服务器异常.在我的dotnet运行控制台命令中,我收到此消息.

fail: Microsoft.AspNetCore.Server.Kestrel[13]
Connection id “0HLAA8HNC511P”,Request id “0HLAA8HNC511P:00000007”: An unhandled exception was thrown by the
application. system.invalidOperationException: Response Content-Length
mismatch: too few bytes written (0 of 9283).

即使通过添加app.UseDeveloperExceptionPage(),我似乎也无法捕获异常.在我的Startup.cs文件调用.

我的控制器方法非常简单,看起来像这样

public async Task<IActionResult> GetAsync([Fromroute] long id,[Fromroute] long fileId,[FromQuery] FilePreviewmodel previewOptions)
{
    var entity = await _fileService.GetAsync(Module,id,fileId);

    var fileName = "MEARS 2000 logo";
    var contentType = "image/gif";

    // this is a byte array
    var data = entity.Data.Content;

    // return file content
    return File(data,contentType,fileName);
}

在IE11中,请求和响应标头看起来像这样.

enter image description here

在chrome中,我的标题看起来像这样.

enter image description here

我已将我的dotnet SDK更新到2.1.3版.

任何人都知道可能会发生什么?

解决方法

我也有这个问题,相反,它不会总是抛出错误.

管理通过删除操作中的标头来解决它,如下所示:

[HttpGet("{container}/{id}")]
public async Task<IActionResult> Get(string container,string id)
{
    /* remove both of these headers... put a warning here to apply the fix after dotnet team distributes the fix. */
    HttpContext.Request.Headers.Remove("If-Modified-Since");
    HttpContext.Request.Headers.Remove("if-none-match");

    var _fileInfo = provider.GetFileInfo($"{container}/{id}");
    if (!_fileInfo.Exists || string.IsNullOrEmpty(id))
        /* return a default file here */

    var last = _fileInfo.LastModified;
    /* ... some code removed for brevity */

    return base.File(_fileInfo.CreateReadStream(),MimeTypeMap.GetMimeType(id.Substring(id.LastIndexOf("."))),lastModified: _lastModified,entityTag: _etag);
}

dotnet –info显示版本:2.0.4

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐