Quantcast
Channel: SharePoint 2013 - Development and Programming forum
Viewing all articles
Browse latest Browse all 7589

Downloading files from a document library

$
0
0

Hello,

I have sorted uploading and deleting files from a SharePoint 2013 Document Library (using CSOM), but I am having problems with the following code to allow users to open / save files.

        public void DownloadDocument(string siteURL, string libraryName, string fileName)
        {
            using (ClientContext clientContext = new ClientContext(siteURL))
            {

                var list = clientContext.Web.Lists.GetByTitle(libraryName);
                clientContext.Load(list.RootFolder);
                clientContext.ExecuteQuery();

                var fi = new FileInfo(fileName);
                var source = String.Format("{0}/{1}/{2}", list.RootFolder.ServerRelativeUrl, null, fi.Name);

                var spFileInfo = Microsoft.SharePoint.Client.File.OpenBinaryDirect(clientContext, source);

                using (var fs = new FileStream(fileName, FileMode.OpenOrCreate))
                {
                    spFileInfo.Stream.CopyTo(fs);
                }
            }
        }

How can I prompt the user to open / save the selected file (this should be the standard browsers download file prompts)? If they open the file it should open in the default application. The files could be any type (.xlsx, .pdf, .docx, .txt, etc.)

Many thanks

Duane

hj


Viewing all articles
Browse latest Browse all 7589

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>