Download file in Lightswitch?

Jan 29 at 3:44 AM

I know this is going to sound stupid, but I got the upload file dialog to work perfectly in my Lightswitch Application, but I can't figure out how to download the file now that I've uploaded it.  I monkeyed around with some different code but no luck.  Help?

Coordinator
Jan 29 at 8:20 PM

Thanks for using HSS Interlink and glad to hear you got the upload working.

As far as downloading, can explain your download scenario? Meaning do you want to download the raw stream into your application code or allow the user to download the file to their desktop?

To download to their desktop you have three options:

1. Download using the Interlink Download Dialog
2. Use the normal Browser (Open Inline) for files like pictures and pdf's
3. Use the normal Browser (Save As) to prompt the user to save the requested file

From the documentation: http://interlink.codeplex.com/documentation

To download a file directly to the browser (Inline) you can use the static method OpenFile

DownloadFileDialog.OpenFileInline(serverSideFileName);

To download a file directly to the browser as an attachment (Save As) you can also use the static method OpenFileAsAttachment
DownloadFileDialog.OpenFileAsAttachment(serverSideFileName);

Jan 29 at 8:51 PM
I just want them to be able to download to their desktop - the documentation doesn't make clear exactly if I need to add a new class for the ability to download as well, or if I can just add the code to a new button in Lightswitch? Sorry if I am being dense.

Jan 29 at 8:54 PM
One other point - I did attempt to create a new DownloadHandler.cs, however, the sample code I looked at was looking for dll's other than the Interlink.dll's I had?
Coordinator
Jan 29 at 10:15 PM

Heres how to get it to work.

const string remoteFileStore = "/interlink/uploads/";
partial void DownloadFile_Execute()
{
	var item = this.Table1Items.SelectedItem;
	if (null == item) return;
	this.DownloadAFile(item.FileName);
}
private void DownloadAFile(string remoteFileName)
{
	var uri = new Uri(string.Format("{0}{1}", remoteFileStore, remoteFileName), UriKind.Relative);
	Dispatchers.Main.Invoke(() =>
	{
		HtmlPage.Window.Navigate(uri, "_new");
	});
}
NOTE: LightSwitch does not support the Interlink DownloadFileDialog.
Coordinator
Jan 29 at 10:24 PM

And to confirm, you do not need the DownloadHandler as you will be downloading the file directly (bypassing Interlink).

Jan 30 at 1:43 AM
Thanks! That worked perfectly - I just needed to add a reference to System.Windows.Browser and I was all set.

Coordinator
Jan 30 at 1:45 AM

Excellent, glad to hear you got it working. And sorry, forget to mentioned LS doesn't include a reference to the Browser dll by default.

Jan 30 at 2:12 AM
Looks like I spoke too soon - it can't find the upload folder. I changed it from the default to my root/uploads, but no luck
Coordinator
Jan 30 at 2:14 AM

The remoteFileStore should match your server side file store. Depending on your project structure it maybe different.

Also mine includes the trailign slash, so make sure the file name you're passing doesn't contain the slash to.

If put a break point on the uri creator, check the resolved value and then manually paste that into the browser and ensure it's correct.

Jan 30 at 3:03 AM
Boy I can't figure this out - it looks like it is pointing to right place

My URL is http://blah.blah:1028/Reporting

sitting on the server at lightswitch/reporting

Coordinator
Jan 30 at 3:04 AM

then your remoteFileStore should "/lightswitch/reporting/interlink/uploads/"

you can go to that physical folder to confirm the path...

 

Jan 30 at 3:15 AM
I really appreciate the help you've given me with this - you don't see this kindof responsiveness very often anymore. I just made a donation and hopefully this works now so you can go do other stuff - great job!
Coordinator
Jan 30 at 3:17 AM

We did receive the donation, and are greatly appreciative. Thank you!

Be sure and check out our blog/forum to stay up to date on all our software

http://highspeed-solutions.net/forum

http://highspeed-solutions.net/blog

 

Jan 30 at 3:34 AM
Glad you got it - sadly, it still didn't work. Sigh - very frustrating.

Do I need to change the handler string at all? I'm at a total loss.
Coordinator
Jan 30 at 3:38 AM

No, the handler has nothing to do with the download.

Can you provide me the absolute path to the folder where the uploaded files are stored?

Example:

c:\inetpub\wwwroot\reporting\interlink\uploads\

If this is your absolute folder path then in LS, your remoteFileStore should be "/reporting/interlink/uploads/"

So to test from the browser you should be able to navigate to

http://yourapp:port/reporting/interlink/uploads/filename.ext

Now you have watch out for sub applications in IIS. If you web app is not a parent application then even though your physical path looks right, IIS is expecting mainapp/subapp/reporting instead of mainapp/reporting

 

 

Jan 30 at 3:47 AM
The file path is c:\Lightswitch\Reporting\Interlink\Uploads
Coordinator
Jan 30 at 3:52 AM

manually put a file in the uploads folder you specified above. something simple like an empty text file (test.txt)

Then open your browser and go to http://localhost:port/reporting/interlink/uploads/test.txt

If that doesn't work try http://localhost:port/interlink/uploads/test.txt

If that doesn't work then restart visual studio and try again.

Once you can manually browse directly to a file in the folder then you can test LS & Interlink.

 

Jan 30 at 4:09 AM
Do I have to convert the final file location to an application?
Coordinator
Jan 30 at 4:10 AM

I'm not sure I understand your question.

Were you able to get to the file from your browser?

Jan 30 at 4:12 AM
Edited Jan 30 at 4:13 AM

No - even if I turn on directory browsing, I can get to the file that way, but can't actually open it.

Jan 30 at 4:17 AM

Ah - I can get to it in the root folder.  Let me try that for now.