Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Tilan 1 post 21 karma points
    Jun 17, 2014 @ 13:56
    Tilan
    0

    How to upload images programmatically into a upload property in umbraco 7 back office content page

    I want to add a image to upload property in a content page programmatically when submitting, how can i do it using contentservice, i can add values to textstring property using setValue but how to do it for  a image? Thanks

  • Dave Woestenborghs 3504 posts 12133 karma points MVP 8x admin c-trib
    Jun 17, 2014 @ 14:05
    Dave Woestenborghs
    0

    Here you have a code snippet. It assumes the file is already uploaded to the server 

    string originalPath ="uploadedImages\\test.png";
       
    IMediaService mediaService =ApplicationContext.Current.Services.MediaService;
       
    var newImage = mediaService.CreateMedia("myTestImage",-1,"Image");
       
    byte[] buffer =System.IO.File.ReadAllBytes(System.IO.Path.GetFullPath(HttpContext.Current.Server.MapPath(originalPath)));
       
    System.IO.MemoryStream strm =newMemoryStream(buffer);
        newImage
    .SetValue("umbracoFile","myNewImage.png", strm);
        mediaService
    .Save(newImage);
       
    if(System.IO.File.Exists(HttpContext.Current.Server.MapPath(originalPath)))
       
    {
         
    System.IO.File.Delete(HttpContext.Current.Server.MapPath(originalPath));
       
    }

    Dave

  • Maxime Besson 2 posts 72 karma points
    Sep 17, 2018 @ 16:11
    Maxime Besson
    0

    I think he asked for a solution to set the value of the content "fileUpload" programmatically. I'm looking for it too.

    Tried node.SetValue("fileUpload", mediaItem);

    didnt work.

  • Zachary Allen 11 posts 82 karma points
    Oct 09, 2019 @ 17:59
    Zachary Allen
    100

    Probably a little late, but for anyone else who stumbles on this question looking for an answer.

    If you're trying to SetValue of a FileUpload property from the Content Service. Here's how I did it.

    This snippet assumes you've already created a new content item with the content service named node. You'll have to add the URL of the file you want to download and the property alias you want to upload the image to.

    string imageUrl = "{URL OF FILE YOU WANT SAVED TO UPLOAD}"
    if (!string.IsNullOrEmpty(imageUrl))
    {
        // Stream File from URL
        var request = WebRequest.Create(imageUrl);
        var webResponse = request.GetResponse();
        var responseStream = webResponse.GetResponseStream();
        IContentTypeBaseServiceProvider contentTypeBaseServiceProvider = Services.ContentTypeBaseServices;
        // Get File Name from Url
        int lastIndex = imageUrl.LastIndexOf("/") + 1;
        string filename = imageUrl.Substring(lastIndex, imageUrl.Length - lastIndex);
    
        if (responseStream != null)
        {
            node.SetValue(contentTypeBaseServiceProvider, "{property alias}", filename, responseStream);
        }
    }
    
Please Sign in or register to post replies

Write your reply to:

Draft