Upload video from custom code

Posted by Community Admin on 03-Aug-2018 19:31

Upload video from custom code

All Replies

Posted by Community Admin on 07-Jun-2011 00:00

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

Posted by Community Admin on 08-Jun-2011 00:00

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

Posted by Community Admin on 08-Jun-2011 00:00

Thank you Lev!!!

This thread is closed