繁体   English   中英

ASP.NET CORE 2 System.Net.Sockets.SocketException:每当上传文件时

[英]ASP.NET CORE 2 System.Net.Sockets.SocketException: Whenever uploading files

当我尝试通过 IIS/服务器上传文件时,我总是收到以下错误消息,它在本地工作,但在服务器上,它总是有这个问题。

有谁知道可能是什么问题?

错误信息:

调用事件处理程序失败:Microsoft.AspNetCore.Connections.ConnectionResetException:远程主机强行关闭了现有连接 ---> System.Net.Sockets.SocketException:Microsoft.AspNetCore 上的远程主机强行关闭了现有连接。 Server.Kestrel.Transport.Sockets.Internal.SocketAwaitable.GetResult() 在 Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets.Internal.SocketConnection.ProcessReceives() 在 Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets.Internal。 SocketConnection.DoReceive() --- 内部异常堆栈跟踪结束 --- 在 System.IO.Pipelines.PipeCompletion.ThrowLatchedException() 在 System.IO.Pipelines.Pipe.GetReadResult(ReadResult& result) 在 System.IO.Pipelines。 Pipe.GetReadAsyncResult() 在 System.IO.Pipelines.Pipe.DefaultPipeReader.GetResult(Int16 token)
在 Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.Http1MessageBody.PumpAsync() 在 System.IO.Pipelines.PipeCompletion.ThrowLatchedException() 在 System.IO.Pipelines.Pipe.GetReadResult(ReadResult& result) 在 System.IO .Pipelines.Pipe.ReadAsync(CancellationToken token) 在 System.IO.Pipelines.Pipe.DefaultPipeReader.ReadAsync(CancellationToken cancelToken) 在 Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.MessageBody.ReadAsync(Memory 1 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.ReadAsyncInternal(Memory 1 缓冲区,CancellationToken 取消令牌)在 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()在 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.SetBodycontext(BaseNet scope, S-Options) Sentry.AspNetCore.SentryMiddleware.PopulateScope(HttpContext context, Scope scope) at Sentry.Scope.Evaluate()

代码隐藏

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)
{

}

根据此 github问题,MSFT 已在 asp.net core 2.1 上修复了此问题。 我建议您可以尝试在您的服务器上更新您的 asp.net 核心运行时版本(至 2.2)。

我也在我这边创建了一个测试演示并发布到 IIS 服务器,它运行良好。

关于我的测试演示的更多细节,您可以参考以下代码:

MVC 视图:

@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>

控制器:

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");
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM