繁体   English   中英

当 ASP.Net Core 应用程序发布到 UAT Web 服务器时,最终用户的文件上传失败

[英]File upload failing for end-user when ASP.Net Core application published to UAT Web Server

我有一个 web 应用程序内置在 ASP.Net Core (3.1) 中,具有文件上传功能。 这已经在本地进行了全面测试,然后在我们的 UAT 中的 web 服务器上发布。 我自己在 UAT 中对其进行了测试,它可以完全正常工作,但是在将其传递给用户测试后,尝试上传文件时会引发错误,我无法确定原因。

我的相关代码以及带有错误的日志文件如下所示:

  • 我主页的文件上传部分:
            <div id="uploader" class="upload-widget">
                @(Html.DevExtreme().FileUploader()
                      .ID("file-uploader")
                      .Multiple(false)
                      .Accept("*")
                      .UploadMode(FileUploadMode.Instantly)
                      .UploadUrl(Url.Action("UploadtoServer", "Home"))
                      .OnValueChanged("fileUploader_valueChanged")
                      .OnUploaded("fileUploader_popUp"))
            </div>
  • 我家 controller 上的方法:
       [HttpPost]
        public ActionResult UploadToServer()
        {

            Log.Info("File selected for upload to server");
            var uploadsPath = Directory.GetCurrentDirectory() + _config["uploadFolder"];
            var inputPath = Directory.GetCurrentDirectory() + _config["inputFolder"];
            var files = Request.Form.Files;


            foreach (var file in files)
            {
                try
                {
                    if (!Directory.Exists(uploadsPath))
                        Directory.CreateDirectory(uploadsPath);

                    using (var fileStream = System.IO.File.Create(Path.Combine(uploadsPath, file.FileName)))
                    {
                        file.CopyTo(fileStream);
                        fileStream.Flush();
                    }

                    Log.Info($"File {file.FileName} successfully uploaded to server");

                    MoveFromUploads(uploadsPath, file);
                    Log.Info($"File {file.FileName} copied across to Input folder");

                    var fullInputFilePath = Path.Combine(inputPath, file.FileName);
                    var loadFile = InitialiseLoadFileModel(fullInputFilePath, file);
                    UploadToDatabase(loadFile);

                }
                catch (Exception e)
                {
                    Log.Error($"File upload from UI has failed: " + e);
                    throw;
                }
            }

            return new EmptyResult();

        }
  • 我的 appsettings.json 的相关部分显示目录配置:
  "uploadFolder": "\\wwwroot\\uploads",
  "inputFolder": "\\wwwroot\\input",
  • 以及用户尝试处理上传后的错误堆栈日志文件:
2020-05-08 20:05:52,667 [32] INFO LGL.FrontEnd.Controllers.HomeController [? ?] - File selected for upload to server
2020-05-08 20:05:52,816 [32] ERROR LGL.FrontEnd.Controllers.HomeController [? ?] - File upload from UI has failed: System.IO.DirectoryNotFoundException: Could not find a part of the path 'C:\Users\zebrowsc\Desktop\DATA RISK.xlsx'.
   at System.IO.FileStream.ValidateFileHandle(SafeFileHandle fileHandle)
   at System.IO.FileStream.CreateFileOpenHandle(FileMode mode, FileShare share, FileOptions options)
   at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options)
   at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize)
   at System.IO.File.Create(String path)
   at LGL.FrontEnd.Controllers.HomeController.UploadToServer() in C:\Users\barremme\source\repos\LGL\LGL.FrontEnd\Controllers\HomeController.cs:line 86
2020-05-08 20:05:52,819 [32] ERROR Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware [? ?] - An unhandled exception has occurred while executing the request.
System.IO.DirectoryNotFoundException: Could not find a part of the path 'C:\Users\zebrowsc\Desktop\DATA RISK.xlsx'.
   at System.IO.FileStream.ValidateFileHandle(SafeFileHandle fileHandle)
   at System.IO.FileStream.CreateFileOpenHandle(FileMode mode, FileShare share, FileOptions options)
   at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options)
   at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize)
   at System.IO.File.Create(String path)
   at LGL.FrontEnd.Controllers.HomeController.UploadToServer() in C:\Users\barremme\source\repos\LGL\LGL.FrontEnd\Controllers\HomeController.cs:line 86
   at lambda_method(Closure , Object , Object[] )
   at Microsoft.Extensions.Internal.ObjectMethodExecutor.Execute(Object target, Object[] parameters)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor.SyncActionResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeActionMethodAsync()
   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeNextActionFilterAsync()
--- End of stack trace from previous location where exception was thrown ---
   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Rethrow(ActionExecutedContextSealed context)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeInnerFilterAsync()
--- End of stack trace from previous location where exception was thrown ---
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeNextResourceFilter>g__Awaited|24_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResourceExecutedContextSealed context)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeFilterPipelineAsync()
--- End of stack trace from previous location where exception was thrown ---
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Awaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope)
   at Microsoft.AspNetCore.Routing.EndpointMiddleware.<Invoke>g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger)
   at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.Session.SessionMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.Session.SessionMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.Builder.Extensions.MapMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware.<Invoke>g__Awaited|6_0(ExceptionHandlerMiddleware middleware, HttpContext context, Task task)
2020-05-08 20:25:59,692 [36] INFO Microsoft.Hosting.Lifetime [? ?] - Application is shutting down...

“C:\Users\zebrowsc\Desktop\DATA RISK.xlsx”显然是浏览器端的本地路径。 这不是服务器可以使用的。

我猜您是在您开发的同一台计算机上以及同一用户上进行测试的? 当然,服务器可以访问那里的本地文件。 并且在开发过程中,大部分限制都被放宽了。

一个稍微好一点的测试设置可以揭示这一点:由于它们很容易受到黑客攻击,网络服务器通常在最严格的用户权限下工作。 特殊用户,对程序和内容目录的最小读取权限。 在这些之下,服务器应该无法找到该文件。

理想的测试应该是两台不同的机器——甚至是虚拟机器——在客户端和服务器之间有额外的层。

暂无
暂无

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

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