I always receive the following error message when I try to upload file via IIS/Server, it works locally but on the server, it always has this problem.
Does anyone have any idea what might be the problem?
Error Message:
Failed invoking event handler: Microsoft.AspNetCore.Connections.ConnectionResetException: An existing connection was forcibly closed by the remote host ---> System.Net.Sockets.SocketException: An existing connection was forcibly closed by the remote host at Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets.Internal.SocketAwaitable.GetResult() at Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets.Internal.SocketConnection.ProcessReceives() at Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets.Internal.SocketConnection.DoReceive() --- End of inner exception stack trace --- at System.IO.Pipelines.PipeCompletion.ThrowLatchedException() at System.IO.Pipelines.Pipe.GetReadResult(ReadResult& result) at System.IO.Pipelines.Pipe.GetReadAsyncResult() at System.IO.Pipelines.Pipe.DefaultPipeReader.GetResult(Int16 token)
at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.Http1MessageBody.PumpAsync() at System.IO.Pipelines.PipeCompletion.ThrowLatchedException() at System.IO.Pipelines.Pipe.GetReadResult(ReadResult& result) at System.IO.Pipelines.Pipe.ReadAsync(CancellationToken token) at System.IO.Pipelines.Pipe.DefaultPipeReader.ReadAsync(CancellationToken cancellationToken) at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.MessageBody.ReadAsync(Memory1 buffer, CancellationToken cancellationToken) at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpRequestStream.ReadAsyncInternal(Memory
1 buffer, CancellationToken cancellationToken) at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpRequestStream.Read(Byte[] buffer, Int32 offset, Int32 count) at Microsoft.AspNetCore.WebUtilities.FileBufferingReadStream.Read(Byte[] buffer, Int32 offset, Int32 count) at System.IO.StreamReader.ReadBuffer() at System.IO.StreamReader.ReadToEnd() at Sentry.Extensibility.DefaultRequestPaylo adExtractor.DoExtractPayLoad(IHttpRequest request) at Sentry.Extensibility.BaseRequestPayloadExtractor.ExtractPayload(IHttpRequest request) at Sentry.Extensibility.RequestBodyExtractionDispatcher.ExtractPayload(IHttpRequest request) at Sentry.AspNetCore.ScopeExtensions.SetBody(BaseScope scope, HttpContext context, SentryAspNetCoreOptions options) at Sentry.AspNetCore.ScopeExtensions.Populate(Scope scope, HttpContext context, SentryAspNetCoreOptions options) at Sentry.AspNetCore.SentryMiddleware.PopulateScope(HttpContext context, Scope scope) at Sentry.Scope.Evaluate()
Code-Behind
try
{
string folder = "FileLocation/";
string folderpath = "";
if (model.Filess != null)
{
string fileExtension = Path.GetExtension(model.Filess.FileName);
fileExtension = fileExtension.ToLower();
long fileSize = model.Filess.Length;
if (fileSize <= 10485760)
{
folderpath = Path.Combine(__hostingEnvironment.WebRootPath, "Uploads", folder);
if (!Directory.Exists(folderpath))
{
Directory.CreateDirectory(folderpath);
}
var parsedContentDisposition =
ContentDispositionHeaderValue.Parse(model.Filess.ContentDisposition);
var filename = Path.Combine(__hostingEnvironment.WebRootPath,
"Uploads", folder, parsedContentDisposition.FileName.Trim('"'));
using (var stream = System.IO.File.OpenWrite(filename))
{
await model.Filess.CopyToAsync(stream);
}
}
};
}
catch (Exception ex)
{
}
According to this github issue , the MSFT has fixed this issue on asp.net core 2.1. I suggest you could try to update your asp.net core runtime version(to 2.2) on your server.
I aslo created a test demo on my side and publish to the IIS server, it works well.
More detais about my test demo, you could refer to below codes:
MVC View:
@model UploadViewModel
@{
ViewData["Title"] = "Index";
}
<h1>Index</h1>
<form asp-action="CreateAsync" enctype="multipart/form-data">
<input asp-for="Id" />
<input asp-for="Filess" />
<input type="submit" />
</form>
Controller:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http.Headers;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using MVCCoreUpload.Models;
namespace MVCCoreUpload.Controllers
{
public class FileUploadController : Controller
{
private readonly IHostingEnvironment _hostingEnvironment;
public FileUploadController(IHostingEnvironment hostingEnvironment)
{
_hostingEnvironment = hostingEnvironment;
}
public IActionResult Index()
{
return View(new UploadViewModel());
}
public async Task<IActionResult> CreateAsync(UploadViewModel model)
{
try
{
string folder = "FileLocation/";
string folderpath = "";
if (model.Filess != null)
{
string fileExtension = Path.GetExtension(model.Filess.FileName);
fileExtension = fileExtension.ToLower();
long fileSize = model.Filess.Length;
if (fileSize <= 10485760)
{
folderpath = Path.Combine(_hostingEnvironment.WebRootPath, "Uploads", folder);
if (!Directory.Exists(folderpath))
{
Directory.CreateDirectory(folderpath);
}
var parsedContentDisposition =
ContentDispositionHeaderValue.Parse(model.Filess.ContentDisposition);
var filename = Path.Combine(_hostingEnvironment.WebRootPath,
"Uploads", folder, parsedContentDisposition.FileName.Trim('"'));
using (var stream = System.IO.File.OpenWrite(filename))
{
await model.Filess.CopyToAsync(stream);
}
}
};
}
catch (Exception ex)
{
}
return View("index");
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.