在進行 Asp.NetCore.MVC 文件上傳時,後臺無法正常讀取文件流保存,出現:Synchronous operations are disallowed. Call WriteAsync or set AllowSynchronousIO to true instead. 查找資料,發現需要 ...
在進行 Asp.NetCore.MVC 文件上傳時,後臺無法正常讀取文件流保存,出現:Synchronous operations are disallowed. Call WriteAsync or set AllowSynchronousIO to true instead.
查找資料,發現需要添加允許條件,才可以; 感謝:https://www.cnblogs.com/lonelyxmas/p/12060869.html
有三種解決方式:第一種:在處理文件的Action 中添加:
var syncIOFeature = HttpContext.Features.Get<IHttpBodyControlFeature>(); if (syncIOFeature != null) { syncIOFeature.AllowSynchronousIO = true; }
第二種:或者在Startup.cs 中註冊
public void ConfigureServices(IServiceCollection services) { // If using Kestrel: services.Configure<KestrelServerOptions>(options => { options.AllowSynchronousIO = true; }); // If using IIS: services.Configure<IISServerOptions>(options => { options.AllowSynchronousIO = true; });
}
第三種:(不太理解。。。)
Request.EnableBuffering(); using (var reader = new StreamReader(Request.Body, encoding: Encoding.UTF8)) { var body = reader.ReadToEndAsync(); // Do some processing with body… // Reset the request body stream position so the next middleware can read it Request.Body.Position = 0; }