Hello,
I created one API which are going to create a document type page in kentico.
here i used one logic of attachments which creating one folder under "C" Directory for storing the downloaded file and that file will insert under kentico. below is the code for that
Code
AttachmentInfo attachment = null;
//Code for download vidoe
//https://www.w3schools.com/html/mov_bbb.mp4
if (!Directory.Exists(@"C:\Video"))
{
Directory.CreateDirectory(@"C:\Video");
}
string filepath = @"C:\Video";
DirectoryInfo dInfo = new DirectoryInfo(filepath);
DirectorySecurity dSecurity = dInfo.GetAccessControl();
dSecurity.AddAccessRule(new FileSystemAccessRule(new SecurityIdentifier(WellKnownSidType.WorldSid, null), FileSystemRights.FullControl, InheritanceFlags.ObjectInherit | InheritanceFlags.ContainerInherit, PropagationFlags.NoPropagateInherit, AccessControlType.Allow));
dInfo.SetAccessControl(dSecurity);
WebClient webClient = new WebClient();
Guid guidKey = Guid.NewGuid();
webClient.DownloadFile(objVideoInfoMember.videoUrl, @"C:\Video\myfile" + guidKey + ".mp4");
// Prepares the path of the file
string file = System.Web.HttpContext.Current.Server.MapPath("C/Video/myfile" + guidKey + ".mp4");
// Inserts the attachment into the "Video" field and updates the page
attachment = DocumentHelper.AddAttachment(newPage, "Video", file, tree);
newPage.Update();
//Delete folder from the directory
if (Directory.Exists(@"C:\Video"))
{
Directory.Delete(@"C:\Video", true);
}
Now all are working fine but when i see the attachment file in kentico it showing file size "0"
can you please help me to resolve this issue. currently i am using kentico 9
Below is entire code
Code
// Creates a new instance of the Tree provider
TreeProvider tree = new TreeProvider(MembershipContext.AuthenticatedUser);
// Gets the current site's root "/" page, which will serve as the parent page
TreeNode parentPage = tree.SelectNodes()
.Path("%/Archived")
.OnSite("Dummy")
.FirstObject;
var categoryId = SettingsHelper.AppSettings["CatgegoryId"];
if (parentPage != null)
{
TreeNode newPage = TreeNode.New("classname.Video", tree);
// Sets the properties of the new page
//newPage.DocumentName = "Video Archived";
newPage.SetValue("title", "Video Archived " + DateTime.Now);
newPage.SetValue("category", categoryId);
newPage.SetValue("description", "Video Archived");
newPage.SetValue("sfvtags", "Archived");
newPage.SetValue("video", Guid.NewGuid());
newPage.SetValue("DocumentPublishFrom", DateTime.Now);
// Inserts the new page as a child of the parent page
newPage.Insert(parentPage, true);
AttachmentInfo attachment = null;
//Code for download vidoe
//https://www.w3schools.com/html/mov_bbb.mp4
if (!Directory.Exists(@"C:\Video"))
{
Directory.CreateDirectory(@"C:\Video");
}
string filepath = @"C:\Video";
DirectoryInfo dInfo = new DirectoryInfo(filepath);
DirectorySecurity dSecurity = dInfo.GetAccessControl();
dSecurity.AddAccessRule(new FileSystemAccessRule(new SecurityIdentifier(WellKnownSidType.WorldSid, null), FileSystemRights.FullControl, InheritanceFlags.ObjectInherit | InheritanceFlags.ContainerInherit, PropagationFlags.NoPropagateInherit, AccessControlType.Allow));
dInfo.SetAccessControl(dSecurity);
WebClient webClient = new WebClient();
Guid guidKey = Guid.NewGuid();
webClient.DownloadFile(objVideoInfoMember.videoUrl, @"C:\Video\myfile" + guidKey + ".mp4");
// Prepares the path of the file
string file = System.Web.HttpContext.Current.Server.MapPath("C/Video/myfile" + guidKey + ".mp4");
// Inserts the attachment into the "Video" field and updates the page
attachment = DocumentHelper.AddAttachment(newPage, "Video", file, tree);
newPage.Update();
//Delete folder from the directory
if (Directory.Exists(@"C:\Video"))
{
Directory.Delete(@"C:\Video", true);
}