Download file in Lightswitch?

Jan 29, 2012 at 4: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?

Jan 29, 2012 at 9: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, 2012 at 9: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, 2012 at 9: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?
Jan 29, 2012 at 11: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.
Jan 29, 2012 at 11:24 PM

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

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

Jan 30, 2012 at 2: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, 2012 at 3: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
Jan 30, 2012 at 3: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, 2012 at 4: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

Jan 30, 2012 at 4:04 AM

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

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

 

Jan 30, 2012 at 4: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!
Jan 30, 2012 at 4: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, 2012 at 4: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.
Jan 30, 2012 at 4: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, 2012 at 4:47 AM
The file path is c:\Lightswitch\Reporting\Interlink\Uploads
Jan 30, 2012 at 4: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, 2012 at 5:09 AM
Do I have to convert the final file location to an application?
Jan 30, 2012 at 5:10 AM

I'm not sure I understand your question.

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

Jan 30, 2012 at 5:12 AM
Edited Jan 30, 2012 at 5: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, 2012 at 5:17 AM

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

Jul 14, 2012 at 7:51 PM
Edited Jul 14, 2012 at 7:52 PM

Hello,

I tried this but it does not work for me:

i'm using a folder share from another server for uploading. \\BG112\Documents

this folder is not in the root of de lightswitch website... is there any way that i can open files with the path \\BG112\Documents\test.pdf

Jul 16, 2012 at 11:23 PM

The challenge that both of you're facing is the abillity to get to a file that will be served up by IIS.

In order for IIS to do so, it needs to know where the files resolve. I think for cfinlan the issue could be resolved with a Virtual Directory point to the folder where the files are stored (plus a bunch of security to protect the documents).

In your case, your challenge is IIS can't map a Virtual Directory to a remote share. The files have to be local. You're going to have to implement some type of File Cache and a customer Download Handler (similar to the Interlink FileDownload handler) that looks for the request file by name in the local file cache (a simple local folder) and if it exists return the cached file. If it doesn't exist, then fetch from the remote files share and copy to the local cache (folder) and return that file.

This can get complicated because now you don't know which file is the correct one. Meaning if the remote file is updated, user will still get the local cached file which is old.

Your options are to no cache, and always return  the remote folder, or to purge/expire the cache at some interval like daily or weekly depending on how often the remote files my be changed.

You could also compare file times of the remote file to the cache file and if it's newer then refresh the cache.

Jul 17, 2012 at 7:05 AM

Thank you for the info,

any examples or links to get me started on that?

Jul 17, 2012 at 11:50 AM

The latest version of Interlink (v3.0) has this caching mechanism that you could leverage; not sure if v3.0 works with LightSwitch though.

You could use the source code to construct your own file caching process.