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

download document programatically on client machine not on server machine

$
0
0

Hi,

I have created custom web part to download document from document library, its downloading document on server path 

not on client path 

like : if user's access portal and choose to download so it should download on his/her machine not on server machine

code :

      public List<clsDocuments> DownloadDocumentsByDocID(int docId, string siteurl, int Isactive)
        {
            List<clsDocuments> lstdoc = new List<clsDocuments>();
            clsGlobal oGlobal = new clsGlobal();
            clsDocuments oDocuments = new clsDocuments();
            try
            {
                SPSecurity.RunWithElevatedPrivileges(delegate ()
                {
                    using (SPSite osite = new SPSite(siteurl))
                    {
                        using (SPWeb sPWeb = osite.RootWeb)
                        {
                            SPList list = sPWeb.Lists.TryGetList(oGlobal.m_lstDejvuDocDepart);
                            if (list != null)
                            {

                                SPQuery osPQuery = new SPQuery();
                                osPQuery.Query = "@<Where><And><Eq><FieldRef Name='ID' /><Value Type='number'>" + docId + "</Value></Eq><Eq><FieldRef Name='Flag'/><Value Type='Number'>" + Isactive + "</Value></Eq></And></Where>";
                                SPListItemCollection oitmcolection = list.GetItems(osPQuery);
                                clsGenric oclsGenri = new clsGenric();
                                foreach (SPListItem pListItem in oitmcolection)
                                {
                                    oDocuments = new clsDocuments();
                                    oDocuments.DocID = Convert.ToInt32(pListItem["ID"]);
                                    oDocuments.DocName = Convert.ToString(pListItem["Filename"]);
                                    oDocuments.DocType = Convert.ToInt32(pListItem["docType"]) == 1 ? "DOC" : "PDF";

                                    if (Convert.ToString(pListItem["status"]).Equals("2"))
                                    {
                                        // Access the file
                                        SPFile file = pListItem.File;
                                        if (file != null)
                                        {
                                            // retrieve the file as a byte array byte[] bArray = file.OpenBinary();
                                            if (!Directory.Exists(@"C:\\" + "DejavaDocuments"))
                                            {
                                                Directory.CreateDirectory(@"C:\\" + "DejavaDocuments");
                                            }
                                            string filePath = Path.Combine("c:\\DejavaDocuments", oDocuments.DocName + "." + oDocuments.DocType);
                                            //open the file stream and write the file
                                            using (FileStream filestream = new FileStream(filePath, FileMode.Create, FileAccess.ReadWrite))
                                            {
                                                byte[] bArray = file.OpenBinary();
                                                filestream.Write(bArray, 0, bArray.Length);
                                                Console.WriteLine("Data Saved");
                                            }
                                        }
                                    }
                                    ///
                                    lstdoc.Add(oDocuments);
                                }

                            }
                        }
                    }
                   
                });
            }
            catch (Exception ex)
            {
                clsErrorLog oError = new clsErrorLog();
                oError.InsertErrorLogs(SPContext.Current.Site.Url, ex.Message, Convert.ToString(ex.InnerException.GetType()), 1, "clsDocuments", "DownloadDocumentsByDocID", SPContext.Current.Web.CurrentUser.LoginName);
            }
            return lstdoc;
        }


Viewing all articles
Browse latest Browse all 7589

Trending Articles



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