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))
{
{
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)
{
clsGenric oclsGenri = new clsGenric();
foreach (SPListItem pListItem in oitmcolection)
{
oDocuments = new clsDocuments();
oDocuments.DocID = Convert.ToInt32(pListItem["ID"]);
oDocuments.DocID = Convert.ToInt32(pListItem["ID"]);
oDocuments.DocName = Convert.ToString(pListItem["Filename"]);
oDocuments.DocType = Convert.ToInt32(pListItem["docType"]) == 1 ? "DOC" : "PDF";
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");
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");
}
}
}
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);
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);
oError.InsertErrorLogs(SPContext.Current.Site.Url, ex.Message, Convert.ToString(ex.InnerException.GetType()), 1, "clsDocuments", "DownloadDocumentsByDocID", SPContext.Current.Web.CurrentUser.LoginName);
}
return lstdoc;
return lstdoc;
}