none
download document programatically on client machine not on server machine

    Question

  • 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;
            }

    Friday, November 9, 2018 1:15 PM

Answers

  •  I have resolved this , in other way

    just pass complete url of file (server + file path)

    and used  window.open() to download document

    anyways thanks all

    • Marked as answer by qasimidl Wednesday, November 14, 2018 6:04 AM
    Wednesday, November 14, 2018 6:04 AM

All replies

  • Hi qasimidl,

    Use CSOM code to download file into Client Machine Path:

                ClientContext ctx = new ClientContext("http://sp/sites/dev/");
                var filerelativeurl = "/sites/dev/Shared Documents/";
                var fileName = "test.jpg";
                string destination = @"C:\Test\";
                DownloadFile(ctx, filerelativeurl, destination, fileName);
    
    
    public static bool DownloadFile(ClientContext context, string fileRelativeUrl, string destinationPath, string fileName)
            {
                try
                {
                    string sourceUrl = fileRelativeUrl + fileName;
                    string completeDestinationPath = destinationPath + fileName;
    
                    if (!System.IO.File.Exists(completeDestinationPath))
                    {
                        Directory.CreateDirectory(destinationPath);
                        FileInformation spFileInfo = Microsoft.SharePoint.Client.File.OpenBinaryDirect(context, sourceUrl);
    
                        using (Stream destination = System.IO.File.Create(completeDestinationPath))
                        {
                            for (int a = spFileInfo.Stream.ReadByte(); a != -1; a = spFileInfo.Stream.ReadByte())
                                destination.WriteByte((byte)a);
                        }
                    }
                }
                catch (Exception ex)
                {
                    return false;
                }
                return true;
            }

    For details, please check my reply in the another thread you posted:

    Guide me whats values pass to this parameter

    Thanks

    Best Regards


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.


    Click here to learn more. Visit the dedicated forum to share, explore and talk to experts about Microsoft Teams.

    Monday, November 12, 2018 8:29 AM
  • Hi qasimild,

    Would you please provide an update of this question ? If the reply is helpful to this question, you could Mark as answer so that it could help others who stuck with the same question in the forum.

    Thanks

    Best Regards


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.


    Click here to learn more. Visit the dedicated forum to share, explore and talk to experts about Microsoft Teams.

    Tuesday, November 13, 2018 12:42 PM
  •  I have resolved this , in other way

    just pass complete url of file (server + file path)

    and used  window.open() to download document

    anyways thanks all

    • Marked as answer by qasimidl Wednesday, November 14, 2018 6:04 AM
    Wednesday, November 14, 2018 6:04 AM