1-888-365-2779
+1-888-365-2779
Try Now
More in this section

Forums / General Discussions / Upload video from custom code

Upload video from custom code

3 posts, 1 answered
  1. Harry
    Harry avatar
    13 posts
    Registered:
    05 Aug 2008
    07 Jun 2011
    Link to this post
    What is missing from this code, every time I upload a wmv file it comes back with "Fail to open media"

    private
    void uploadVideo(Telerik.Sitefinity.Modules.Libraries.LibrariesManager libManager, Telerik.Sitefinity.Libraries.Model.VideoLibrary videoLib, Telerik.Web.UI.UploadedFile uploadedFile)
            {
                try
                {
                    if (libManager != null && videoLib != null && uploadedFile != null)
                    {
                        // Do a look up for existing video
                        Telerik.Sitefinity.Libraries.Model.Video videoToModify = libManager.GetVideos().Where(d => d.Title == uploadedFile.GetNameWithoutExtension().ToString() && d.Status == Telerik.Sitefinity.GenericContent.Model.ContentLifecycleStatus.Live).OrderByDescending(d => d.LastModified).FirstOrDefault();
                        if (videoToModify == null)
                        {
                            // No videos were found, create a new one.
                            Telerik.Sitefinity.Libraries.Model.Video video = libManager.CreateVideo();
                            //set the newly created video
                            video.Parent = videoLib; // Sets the Library Name
                            video.Title = uploadedFile.GetNameWithoutExtension(); // Sets the file name
                            video.Visible = true; // Sets visible flag
                            video.Author = HttpContext.Current.User.Identity.Name.ToString(); // set the author name
                            video.Description = uploadedFile.GetNameWithoutExtension(); // Set the description
                            video.UrlName = uploadedFile.GetNameWithoutExtension(); // sets the page url
                            video.ApprovalWorkflowState = "Published"; // Sets the video to publish instead of draft by default
                            if (video.Status == Telerik.Sitefinity.GenericContent.Model.ContentLifecycleStatus.Temp)
                            {
                                video.Status = Telerik.Sitefinity.GenericContent.Model.ContentLifecycleStatus.Live;
                            }
                            byte[] buffer = new byte[uploadedFile.InputStream.Length];
                            long position = 0;
                            int currentByte = uploadedFile.InputStream.ReadByte();
                            while (currentByte != -1)
                            {
                                buffer[position++] = (byte)currentByte;
                                currentByte = uploadedFile.InputStream.ReadByte();
                            }                       
                            System.IO.MemoryStream ms = new System.IO.MemoryStream(buffer);
                            //upload the video content
                            libManager.Upload(video, ms, this.fileUpload.UploadedFiles[0].GetExtension());
                            // the item is in Draft State. We hae to call Publish method to publish it.                           
                            libManager.Publish(video);
                            libManager.SaveChanges();
                        }
                }
    }
  2. Lev Rosenblit
    Lev Rosenblit avatar
    37 posts
    Registered:
    14 May 2008
    08 Jun 2011
    Link to this post
    Hi,
    The following code worked for me:

    private void uploadVideo(VideoLibrary videoLib, UploadedFile uploadedFile)
    {
        LibrariesManager libManager = LibrariesManager.GetManager();
        libManager.Provider.SuppressSecurityChecks = true;
        Telerik.Sitefinity.Libraries.Model.Video newVid = libManager.CreateVideo();
        newVid.Parent = videoLib;
        var vidName = uploadedFile.GetNameWithoutExtension();
        newVid.Title = vidName;
        newVid.UrlName = vidName;
        libManager.Upload(newVid, uploadedFile.InputStream, uploadedFile.GetExtension());
        libManager.RecompileItemUrls<Telerik.Sitefinity.Libraries.Model.Video>(newVid);
        libManager.Publish(newVid);
        libManager.SaveChanges();
    }


    Lev Rosenblit
    Answered
  3. Harry
    Harry avatar
    13 posts
    Registered:
    05 Aug 2008
    08 Jun 2011
    Link to this post
    Thank you Lev!!!
3 posts, 1 answered