The user of a LightSwitch intranet application needs a button to select a file from his pc and copy it over to the Windows server where another process will pick it up and process it.
I have read a lot of articles about uploading downloading files in LightSwitch for web based and desktop based apps. Here is what works for me. The solution comes from an article by Dan Seefeldt.
The user needs a button on one of his screens with which he can select a file that will then be copied by the application into a particular location on a Windows server. Clicking on the button will open an open file dialog, after selecting the file and clicking on the Open button, the app will copy the selected file into another windows folder.
- to create the button on one of the screens, with the text ‘Load File’
- In VS open the screen design, under ‘Rows layout / Screen command bar’ click on ‘+ Add’ to add a new button. the button is called ‘LoadFile’. By using this naming convention, it will be displayed with spaces between the words.
- To add code behind the button, right click on the button ‘Load file’ and from the popup menu select ‘Edit Execute Code’. This will open the event handler code that handles the button click event.
- the method LoadFile_Execute() will be created, I have added 2 using statements for the openfiledialog windows control and for the Ligthswitch threading.
- The previous code only shows the file object being selected, there is no code yet to copy the file to another folder. The code shown is rudimentary.
The file copying works.
The above code will work in the protected area of LightSwitch, for instance in My Documents on your pc, it will not work on an external or on network drive. To fix this problem I simply had to write an Windows Forms (or WPF) app that I launch from the LightSwitch command button, this app then opens the OpenFileDialog and copies the file.