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

Forums / Developing with Sitefinity / Custom Libary Upload using telerik:radupload

Custom Libary Upload using telerik:radupload

9 posts, 0 answered
  1. derek
    derek avatar
    37 posts
    Registered:
    15 Jun 2007
    03 Apr 2009
    Link to this post
    I'm working on a custom upload form for our users so that they don't have to go into the admin, and also force them to select the proper categories for their files.

    I'm using the libraryManager in sitefinity, but I can't find the best way to upload a file programatically.

    I'm hoping that there might be an easy way for the sitefinity library to recognize a telerik: radUpload "UploadedFile" object without too much coding.
  2. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    25 Nov 2016
    04 Apr 2009
    Link to this post
    Hi derek,

    Here is a sample that demonstrates uploading files to Images and Documents library  and the file system as well using RadUpload control.

            // upload files using RadUpload     
            foreach (UploadedFile file in RadUpload1.UploadedFiles)     
            {     
                file.SaveAs("c:\\Images\\" + file.GetName(), true);     
            }     
            // create new instance of LibraryManager     
            LibraryManager libraryManager = new LibraryManager();     
            // get library by specified Name     
            ILibrary parentLibrary = libraryManager.GetLibrary("Images");     
            // create an image content item     
            IContent testContent = libraryManager.CreateContent("image/jpeg");     
            // specify parent library the image will belong to     
            testContent.ParentID = parentLibrary.ID;     
            //get the first uploaded file     
            UploadedFile firstFile = RadUpload1.UploadedFiles[0];     
            byte[] buffer = new byte[firstFile.InputStream.Length];     
            long position = 0;     
            int currentByte = firstFile.InputStream.ReadByte();     
            while (currentByte != -1)     
            {     
                buffer[position++] = (byte)currentByte;     
                currentByte = firstFile.InputStream.ReadByte();     
            }     
            IContent content = null;     
            // upload the file     
                 
            try     
            {     
                content = libraryManager.UploadFile(buffer,     
                firstFile.GetNameWithoutExtension(),     
                firstFile.GetExtension(),     
                firstFile.ContentType,     
                firstFile.ContentLength,     
                parentLibrary);     
         
                StagedContent temp = libraryManager.GetCurrentState(content.ID) as StagedContent;     
                temp.SetMetaData("Author""myself");     
                libraryManager.SaveContent(temp, ContentStatus.Published);     
                     
            }     
            catch (ArgumentException ex)     
            {     
                Response.Write("There is an exception");     
            }    

    I hope this helps.

    All the best,
    Ivan Dimitrov
    the Telerik team

    Check out Telerik Trainer , the state of the art learning tool for Telerik products.
  3. gopalraman gopal
    gopalraman gopal avatar
    52 posts
    Registered:
    03 Dec 2009
    15 Jan 2010
    Link to this post
    Hi

           While uploading and converting .eps file to .jpeg i got the error in following line

     content = libraryManager.UploadFile(buffer,
                firstFile.GetNameWithoutExtension(),
                firstFile.GetExtension(),
                firstFile.ContentType,
                firstFile.ContentLength,
                parentLibrary);


    pls help me.
  4. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    25 Nov 2016
    15 Jan 2010
    Link to this post
    Hello gopalraman gopal,

    What is the error you are getting? There should not be a problem in our code. Most probably your extension is not correct or you are passing incorrect parameters to UploadFile method.

    Kind regards,
    Ivan Dimitrov
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
  5. gopalraman gopal
    gopalraman gopal avatar
    52 posts
    Registered:
    03 Dec 2009
    15 Jan 2010
    Link to this post
    hi
     i am getting "Parameter is not valid." error.
     Can u send the sample code with .eps file pls.



    Thank's
    G.GopalRaman
  6. Radoslav Georgiev
    Radoslav Georgiev avatar
    3370 posts
    Registered:
    01 Feb 2016
    15 Jan 2010
    Link to this post
    Hi gopalraman gopal,

    How do you convert the eps file to a jpeg file? Sitefinity does not have built in functionality to do this. This means that you have to convert the file yourself. Otherwise you can upload files with .eps file format in a library which is not of Image Library type. You have to create a Custom library and upload the .eps files in this one. Sample code bellow is for creating a custom library and uploading the eps file:
    <telerik:RadUpload id="RadUpload1" runat="server" initialfileinputscount="3" TargetFolder="~/Files"/>
    <asp:button id="buttonSubmit" runat="server" cssclass="RadUploadButton"  text="Submit" style="MARGIN-TOP: 6px" />

    protected void Page_Load(object sender, EventArgs e)
        {
            this.buttonSubmit.Click += new EventHandler(buttonSubmit_Click);
        }
     
        void buttonSubmit_Click(object sender, EventArgs e)
        {
            // upload files using RadUpload
            //foreach (Telerik.Web.UI.UploadedFile file in RadUpload1.UploadedFiles)
            //{
            //    file.SaveAs("/Files/" + file.GetName(), true);
            //}
     
            // create new instance of LibraryManager
            LibraryManager libraryManager = new LibraryManager();
            //create new library
            ILibrary customLibrary = libraryManager.CreateLibrary();
            customLibrary.TypeName = "Custom";
            customLibrary.Name = "My Library";
            libraryManager.SaveLibrary(customLibrary);
            //get the first uploaded file
            Telerik.Web.UI.UploadedFile firstFile = RadUpload1.UploadedFiles[0];
            byte[] buffer = new byte[firstFile.InputStream.Length];
            long position = 0;
            int currentByte = firstFile.InputStream.ReadByte();
            while (currentByte != -1)
            {
                buffer[position++] = (byte)currentByte;
                currentByte = firstFile.InputStream.ReadByte();
            }
            //create an eps content item
            IContent testContent = libraryManager.CreateContent(firstFile.ContentType);
     
            // specify parent library the eps file will belong to
            testContent.ParentID = customLibrary.ID;
            IContent content = null;
            // upload the file
            try
            {
                content = libraryManager.UploadFile(buffer,
                firstFile.GetNameWithoutExtension(),
                firstFile.GetExtension(),
                firstFile.ContentType,
                firstFile.ContentLength,
                customLibrary);
            }
     
            catch (ArgumentException ex)
            {
                Response.Write("There is an exception");
            }
            // save the image as a content of the file
            testContent.Content = buffer;
            Response.Write(testContent.GetMetaData("Name") + "<br />");
        }

    You can also extend the Images type library to allow .eps file formats. Then you can upload .eps files in image libraries:
    <libraries defaultGenericProvider="Libraries">
        <genericContentProviders>
            <add providerName="Libraries" publicationDateField="Publication_Date"/>
        </genericContentProviders>
        <libraryInfo>
            <add name="Image" title="Image Library" defaultExtenstions=".png, .jpg, .jpeg, .gif, .eps" metaKeys="Author, AlternateText, Extension, Name, Height, Width, Size, Category"></add>


    Regards,
    Radoslav Georgiev
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
  7. gopalraman gopal
    gopalraman gopal avatar
    52 posts
    Registered:
    03 Dec 2009
    15 Jan 2010
    Link to this post
    Hi Radoslav Georgiev ,

                           You cleared my doubt.thanks for your fast reply.

    Thank's
  8. Ano
    Ano avatar
    1 posts
    Registered:
    09 Jun 2011
    30 Sep 2011
    Link to this post
    how we can get the height and width of a image at the time of uploading using rad upload control.
    iam getting only the size,not getting the height and width
  9. Radoslav Georgiev
    Radoslav Georgiev avatar
    3370 posts
    Registered:
    01 Feb 2016
    04 Oct 2011
    Link to this post
    Hi Ano,

    You can get the image width and height by instantiating an image object from the uploaded file. To do so you must read the file's binary data and use something like bellow:
    System.Drawing.Image img = System.Drawing.Image.FromStream(new MemoryStream(buffer));

    Then you can get the Width and Height properties of the image.

    The other option is to instantiate the image from the uploaded file:
    System.Drawing.Image img = System.Drawing.Image.FromFile("PathToUploadedFile");


    All the best,
    Radoslav Georgiev
    the Telerik team
    Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Register for webinar
9 posts, 0 answered