Added new file (.mp4) but not getting attachment size

Divya Mawar asked on September 6, 2017 14:27

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

Recent Answers


Chetan Sharma answered on September 6, 2017 14:32

Hi,

On what line are you getting this error? Second, is your code working for a smaller size file, if that's the case, or any image files.

Could there be possibility of the size of the mp4 file? I cannot locate code where it is throwing error and neither you have specified it.

Thanks, Chetan

0 votesVote for this answer Mark as a Correct answer

Divya Mawar answered on September 6, 2017 14:42

Hello Chetan,

I am not getting any error, and there is no limitation for file size. video is uploaded successfully but when i see in kentico page so there is file size was "0"

Thanks

0 votesVote for this answer Mark as a Correct answer

Chetan Sharma answered on September 6, 2017 15:14

You are adding these videos as attachments to a page which you can see through Attachment tab or control on the Kentico CMS? So what I understand is that File is getting uploaded but the size is showing as zero?

How large is the file? Does it happen for small sized files too?

0 votesVote for this answer Mark as a Correct answer

Suneel Jhangiani answered on September 6, 2017 16:33 (last edited on September 6, 2017 16:33)

I think you will have to update the size after adding the attachment:

// Inserts the attachment into the "Video" field and updates the page
attachment = DocumentHelper.AddAttachment(newPage, "Video", file, tree);
attachment.AttachmentSize = attachment.AttachmentBinary.Length;
AttachmentInfoProvider.SetAttachmentInfo(attachment)
newPage.Update();
0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.