加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-15 22:49:10 所属栏目:百科 来源:网络整理
导读:我有一个返回文件的控制器方法,并且在所有浏览器中,这与IE11不同.在IE11中,我得到500服务器异常.在我的dotnet运行控制台命令中,我收到此消息. fail: Microsoft.AspNetCore.Server.Kestrel[13] Connection id “0HLAA8HNC511P”,Request id “0HLAA8HNC511P:0
我有一个返回文件的控制器方法,并且在所有浏览器中,这与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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读