New-UDUpload - only works once?

There’s a great opportunity that I’m doing something wrong here, but I’m having trouble with uploading more than one file…

I’ve simplified it down and found the dashboard stops working only when I write the file to disk. If I comment out the writing of the file, then everything behaves as expected (i.e. other buttons on the dashboard continue to function, subsequent uploads show in the toast messages correctly).

New-UDUpload -Text "Upload2" -Id "u2" -OnUpload {
	Show-UDToast $Body -Duration 8000
	$BodyFromJSON = $Body | ConvertFrom-Json
	Show-UDToast $BodyFromJSON.Name -Duration 8000
	SaveBase64ToUploadsFolder -filename $BodyFromJSON.Name -filebase64 $BodyFromJSON.Data
}

Previously I had the file save/write code in the -OnUpload block but thought maybe that was causing the problem so moved it out into its own function…

function SaveBase64ToUploadsFolder {
    param (
        $filename,
        $filebase64
    )

[byte[]]$bytes = [Convert]::FromBase64String($filebase64)
$filepath = "C:\ProgramData\UniversalAutomation\Repository\uploads\$filename"
[System.IO.File]::WriteAllBytes($filepath, $bytes)

Show-UDToast "$filename processed in function, saved to: $filepath" -Duration 8000}

It’s this line here: [System.IO.File]::WriteAllBytes($filepath, $bytes)
If I comment that out = all ok. As soon as that line runs (and it does create the file ok), no further uploads work, as in no toast messages appear and no files are created. As well as all the other buttons on the dashboard not performing as they should (but still remain clickable).

And then if I refresh the dashboard, the buttons on the dashboard work, and the file upload and write works again (but only the once).

Any ideas?

Cheers!
Steve

Can you check for errors in the browser (Press F12 and check out the console) and look for errors in the log (%ProgramData%\PowerShellUniversal). That’s pretty strange.

I’ve opened a bug to track this: https://github.com/ironmansoftware/powershell-universal/issues/27

No errors in either with console output.

%ProgramData%\PowerShellUniversal log I see…

2020-08-10T08:25:29.1590380+12:00 0HM1SJUE52J5Q:0000004A [INF] Request starting HTTP/1.1 GET http://servername.com/api/internal/component/element/Time   (ca22a1cb)

2020-08-10T08:25:29.1594561+12:00 0HM1SJUE52J5Q:0000004A [INF] Executing endpoint ‘“UniversalDashboard.Controllers.ComponentController.Element (Universal.Server)”’ (500cc934)
2020-08-10T08:25:29.1595197+12:00 0HM1SJUE52J5Q:0000004A [INF] Route matched with “{action = “Element”, controller = “Component”}”. Executing controller action with signature “System.Threading.Tasks.Task1[Microsoft.AspNetCore.Mvc.IActionResult] Element(System.String)" on controller "UniversalDashboard.Controllers.ComponentController" ("Universal.Server"). (122b2fdf) 2020-08-10T08:25:29.1600019+12:00 0HM1SJUE52J5Q:0000004A [INF] Executed action "UniversalDashboard.Controllers.ComponentController.Element (Universal.Server)" in 0.451ms (afa2e885) 2020-08-10T08:25:29.1600558+12:00 0HM1SJUE52J5Q:0000004A [INF] Executed endpoint '"UniversalDashboard.Controllers.ComponentController.Element (Universal.Server)"' (99874f2b) 2020-08-10T08:25:29.1618118+12:00 0HM1SJUE52J5Q:0000004A [ERR] An unhandled exception has occurred while executing the request. (48a46595) System.NullReferenceException: Object reference not set to an instance of an object. at UniversalDashboard.Controllers.ComponentController.RunScript(String endpointId, Dictionary2 parameters) in D:\a\universal\universal\src\Universal.Server\Controllers\ComponentController.cs:line 141
at UniversalDashboard.Controllers.ComponentController.Element(String id) in D:\a\universal\universal\src\Universal.Server\Controllers\ComponentController.cs:line 197
at Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor.TaskOfIActionResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.g__Awaited|12_0(ControllerActionInvoker invoker, ValueTask1 actionResultValueTask) at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeNextActionFilterAsync>g__Awaited|10_0(ControllerActionInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted) 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__Logged|17_1(ResourceInvoker invoker) at Microsoft.AspNetCore.Routing.EndpointMiddleware.<Invoke>g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger) at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context) at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context) at Universal.Server.Middleware.RoutingMiddleware.Invoke(HttpContext httpContext, IPolicyEvaluator policyEvaluator) in D:\a\universal\universal\src\Universal.Server\Middleware\RoutingMiddleware.cs:line 88 at Swashbuckle.AspNetCore.SwaggerUI.SwaggerUIMiddleware.Invoke(HttpContext httpContext) at Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.Invoke(HttpContext httpContext, ISwaggerProvider swaggerProvider) at Microsoft.AspNetCore.Session.SessionMiddleware.Invoke(HttpContext context) at Microsoft.AspNetCore.Session.SessionMiddleware.Invoke(HttpContext context) at Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware.<Invoke>g__Awaited|6_0(ExceptionHandlerMiddleware middleware, HttpContext context, Task task) 2020-08-10T08:25:29.1622187+12:00 0HM1SJUE52J5Q:0000004A [INF] /api/internal/component/element/Time (63040fa3) 2020-08-10T08:25:29.1623233+12:00 0HM1SJUE52J5Q:0000004A [INF] C:\ProgramData\PowerShellUniversal\Dashboard\Frameworks\UniversalDashboard\3.0.0\apiinternalcomponentelementTime (b802df94) 2020-08-10T08:25:29.1625522+12:00 0HM1SJUE52J5Q:0000004A [INF] Request finished in 3.5363ms 200 text/html (791a596a) 2020-08-10T08:25:29.6273508+12:00 0HM1SJUE52J6I:0000004A [INF] Request starting HTTP/1.1 GET http://servername.com/api/internal/component/element/Time (ca22a1cb) 2020-08-10T08:25:29.6277746+12:00 0HM1SJUE52J6I:0000004A [INF] Executing endpoint '"UniversalDashboard.Controllers.ComponentController.Element (Universal.Server)"' (500cc934) 2020-08-10T08:25:29.6278383+12:00 0HM1SJUE52J6I:0000004A [INF] Route matched with "{action = \"Element\", controller = \"Component\"}". Executing controller action with signature "System.Threading.Tasks.Task1[Microsoft.AspNetCore.Mvc.IActionResult] Element(System.String)” on controller “UniversalDashboard.Controllers.ComponentController” (“Universal.Server”). (122b2fdf)
2020-08-10T08:25:29.6282439+12:00 0HM1SJUE52J6I:0000004A [INF] Executed action “UniversalDashboard.Controllers.ComponentController.Element (Universal.Server)” in 0.3748ms (afa2e885)
2020-08-10T08:25:29.6282982+12:00 0HM1SJUE52J6I:0000004A [INF] Executed endpoint ‘“UniversalDashboard.Controllers.ComponentController.Element (Universal.Server)”’ (99874f2b)
2020-08-10T08:25:29.6300296+12:00 0HM1SJUE52J6I:0000004A [ERR] An unhandled exception has occurred while executing the request. (48a46595)
System.NullReferenceException: Object reference not set to an instance of an object.
at UniversalDashboard.Controllers.ComponentController.RunScript(String endpointId, Dictionary2 parameters) in D:\a\universal\universal\src\Universal.Server\Controllers\ComponentController.cs:line 141 at UniversalDashboard.Controllers.ComponentController.Element(String id) in D:\a\universal\universal\src\Universal.Server\Controllers\ComponentController.cs:line 197 at Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor.TaskOfIActionResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments) at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeActionMethodAsync>g__Awaited|12_0(ControllerActionInvoker invoker, ValueTask1 actionResultValueTask)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.g__Awaited|10_0(ControllerActionInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
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.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.g__Logged|17_1(ResourceInvoker invoker)
at Microsoft.AspNetCore.Routing.EndpointMiddleware.g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger)
at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)
at Universal.Server.Middleware.RoutingMiddleware.Invoke(HttpContext httpContext, IPolicyEvaluator policyEvaluator) in D:\a\universal\universal\src\Universal.Server\Middleware\RoutingMiddleware.cs:line 88
at Swashbuckle.AspNetCore.SwaggerUI.SwaggerUIMiddleware.Invoke(HttpContext httpContext)
at Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.Invoke(HttpContext httpContext, ISwaggerProvider swaggerProvider)
at Microsoft.AspNetCore.Session.SessionMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.Session.SessionMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware.g__Awaited|6_0(ExceptionHandlerMiddleware middleware, HttpContext context, Task task)
2020-08-10T08:25:29.6304423+12:00 0HM1SJUE52J6I:0000004A [INF] /api/internal/component/element/Time (63040fa3)
2020-08-10T08:25:29.6305563+12:00 0HM1SJUE52J6I:0000004A [INF] C:\ProgramData\PowerShellUniversal\Dashboard\Frameworks\UniversalDashboard\3.0.0\apiinternalcomponentelementTime (b802df94)
2020-08-10T08:25:29.6307682+12:00 0HM1SJUE52J6I:0000004A [INF] Request finished in 3.4357ms 200 text/html (791a596a)

Solved.

It was because I was writing the uploaded file into a subfolder in the “Repository” directory (C:\ProgramData\UniversalAutomation\Repository\uploads). I guess this must be a monitored location and when the new file was written, causing a problem. I changed the directory where I was writing the file to (to C:\testupload, just to see if this was the issue) and now the dashboard remains working after 1 upload, and subsequent uploads continue to work with the file written into C:\testupload correctly!

2 other things I’ve come across with the New-UDUpload…

example one

  1. click upload button, choose file “1.jpg”
  2. click open
  3. -OnUpload fires OK (can display toast message with filename)
  4. click the upload button, choose any file (could be the same “1.jpg”)
  5. click cancel = the upload button loses its text

example two

  1. click upload button, choose file “1.jpg”
  2. click open
  3. -OnUpload fires OK (can display toast message with filename)
  4. click the upload button, choose the same file “1.jpg”
  5. click open = nothing happens, -OnUpload not firing?

example three (same as two with extra)

  1. click upload button, choose file “1.jpg”
  2. click open
  3. -OnUpload fires OK (can display toast message with filename)
  4. click the upload button, choose the same file “1.jpg”
  5. click open = nothing happens, -OnUpload not firing?
  6. click the upload button, choose any other file other than “1.jpg”, so “2.jpg”
  7. click open
  8. -OnUpload fires OK (can display toast message with filename)
  9. click the upload button, choose the first file “1.jpg” again
  10. click open
  11. -OnUpload fires OK (can display toast message with filename)

These may well be due to something I’m doing incorrectly with my dashboard setup - easy to test from another server though to see if anyone else experiences the same.

Hi
[update]
For chrome (v87.0.4280.88) it does not work for c:\temp,
For firefox (v83.0) it works ok for c:\temp
[end update]

I get the same issues, to get a file to load more than once I have to

1.click the dashboard upload button
2.click the cancel button when prompted in the file upload dialogue
3.click the dashboard upload button again (that now has no label - BUG)
4.upload works

Using
C:\ProgramData\PowerShellUniversal\UniversalDashboard\Frameworks\v3

And sending file to c:\temp