public class DownloadFile
{
Logger logger = new Logger();
public void FileRef(ListItem item, ClientContext clientContext)
{
try
{
string strFileRelativeUrl = string.Empty;
string tempLocation = CreateDirectory();
if (item != null)
{
strFileRelativeUrl = item["FileRef"].ToString();
string fileName = strFileRelativeUrl.Substring(strFileRelativeUrl.LastIndexOf('/') + 1);
if (!string.IsNullOrEmpty(strFileRelativeUrl))
{
// string fileName = System.IO.Path.GetFileName(mf.FileRef);
clientContext.ExecuteQuery();
//download
FileInformation fromFileInfo = Microsoft.SharePoint.Client.File.OpenBinaryDirect(clientContext, strFileRelativeUrl);
clientContext.ExecuteQuery();
Stream fs = Microsoft.SharePoint.Client.File.OpenBinaryDirect(clientContext, strFileRelativeUrl).Stream;
clientContext.ExecuteQuery();
byte[] binary = ReadFully(fs);
FileStream stream = new FileStream(tempLocation + "\\" + fileName, System.IO.FileMode.Create);
BinaryWriter writer = new BinaryWriter(stream);
writer.Write(binary);
writer.Close();
}
}
}
catch (Exception exec)
{
logger.WriteEventLog("Please contact admin : " + exec.Message);
throw exec;
}
}
public static byte[] ReadFully(Stream input)
{
byte[] buffer = new byte[16 * 1024];
using (MemoryStream ms = new MemoryStream())
{
int read;
while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
{
ms.Write(buffer, 0, read);
}
return ms.ToArray();
}
}
public string CreateDirectory()
{
string fileDirectory = ConfigurationManager.AppSettings["FileLocation"];
if (!Directory.Exists(fileDirectory))
{
Directory.CreateDirectory(fileDirectory);
}
return fileDirectory;
}
}
{
Logger logger = new Logger();
public void FileRef(ListItem item, ClientContext clientContext)
{
try
{
string strFileRelativeUrl = string.Empty;
string tempLocation = CreateDirectory();
if (item != null)
{
strFileRelativeUrl = item["FileRef"].ToString();
string fileName = strFileRelativeUrl.Substring(strFileRelativeUrl.LastIndexOf('/') + 1);
if (!string.IsNullOrEmpty(strFileRelativeUrl))
{
// string fileName = System.IO.Path.GetFileName(mf.FileRef);
clientContext.ExecuteQuery();
//download
FileInformation fromFileInfo = Microsoft.SharePoint.Client.File.OpenBinaryDirect(clientContext, strFileRelativeUrl);
clientContext.ExecuteQuery();
Stream fs = Microsoft.SharePoint.Client.File.OpenBinaryDirect(clientContext, strFileRelativeUrl).Stream;
clientContext.ExecuteQuery();
byte[] binary = ReadFully(fs);
FileStream stream = new FileStream(tempLocation + "\\" + fileName, System.IO.FileMode.Create);
BinaryWriter writer = new BinaryWriter(stream);
writer.Write(binary);
writer.Close();
}
}
}
catch (Exception exec)
{
logger.WriteEventLog("Please contact admin : " + exec.Message);
throw exec;
}
}
public static byte[] ReadFully(Stream input)
{
byte[] buffer = new byte[16 * 1024];
using (MemoryStream ms = new MemoryStream())
{
int read;
while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
{
ms.Write(buffer, 0, read);
}
return ms.ToArray();
}
}
public string CreateDirectory()
{
string fileDirectory = ConfigurationManager.AppSettings["FileLocation"];
if (!Directory.Exists(fileDirectory))
{
Directory.CreateDirectory(fileDirectory);
}
return fileDirectory;
}
}
No comments:
Post a Comment