Copied to clipboard

Flag this post as spam?

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


  • Peter Alcock 113 posts 176 karma points
    Apr 20, 2016 @ 14:24
    Peter Alcock
    0

    Hi all,

    Im trying to create the ability for logged on 'Members' to upload an image from the front end, i have spent god knows how long trawling through various ways and come up empty.

    My umbraco version was installed via webmatrix not VS, does anyone have a way of doing this via partial view macro? or a complete solution already i can use (DLL's etc).

    Any help apreciated!

    Thanks Pete

  • Bill Haggerty 43 posts 177 karma points
    Apr 20, 2016 @ 18:26
    Bill Haggerty
    0

    Peter, I am pretty new to Umbraco, so I cannot speak to the specifics of using the Web API, with Umbraco. But I have done something similar with regular .Net Web API.

    What I am suggesting is that you google 'multipartstreamprovider'. That class is the basis along with using the Umbraco way of doing Web API is the backend part that handles the POST.

    The part that does the POST would be a regular JavaScript ajax POST from your view.

    If you google 'multipartstreamprovider strathweb', the August 29 post has a pretty good overview of how file uploads can work with Web API.

    Hope that helps !! Bill

  • Peter Alcock 113 posts 176 karma points
    Apr 21, 2016 @ 10:42
    Peter Alcock
    1

    Managed to crack it! And have it as a multiple image uploader. Here is the code for future reference, it is all based inside a partial view macro file.

    @using umbraco.cms.businesslogic.web
    @using System.Web.Security
    @* A Razor script to add a basic comment function to Umbraco pages
        You need a document type called Comment with a textstring property
        called commentName and a textbox multiple property called commentMessage
        You also need to allow the Comment document type as child document type
        to your textpage document type.
        Create this script with a macro and add it below the bodyText in your
        template(s) *@
    @* Some global variables *@
    @{
        var successfulPost = false;
        var name = Request["name"];
        var message = Request["message"];
        var parent = Request["parent"];
        var price = Request["price"];
    }
    @* Handle form post: create a new document if a valid post*@
    @if (IsPost)
    {
        if (name != "" && message != "" && parent != "")
        {
            var dt = DocumentType.GetByAlias("Deal");
    
            @* Make sure its a valid document type alias *@
    
    if (dt != null)
    {
    
        var author = umbraco.BusinessLogic.User.GetUser(0);
        var member = Membership.GetUser();
    
        var doc = Document.MakeNew(name, dt, author, Convert.ToInt32(parent));
        doc.getProperty("name").Value = name;
        doc.getProperty("mainText").Value = message;
        doc.getProperty("price").Value = price;
        doc.getProperty("client").Value = Convert.ToString(member);
    
        var imageIds = "";
        var mediaFiles = Request.Files;
        if (ApplicationContext.Current != null && mediaFiles!=null && mediaFiles.Count>0)
        {
            var ms = ApplicationContext.Current.Services.MediaService;
            for (int i = 0; i < mediaFiles.Count; i++)        
            {
                HttpPostedFileBase mediaFile = mediaFiles[i];
                if (mediaFile != null && mediaFile.ContentLength > 0)
                {
                    var newFile = ms.CreateMedia(mediaFile.FileName, -1, "Image");
                    newFile.SetValue("umbracoFile", mediaFile);
                    ms.Save(newFile);
                    imageIds += newFile.Id + ",";
                }
            }
        }
    
        if (!string.IsNullOrEmpty(imageIds))
        {
            doc.getProperty("images").Value = imageIds.TrimEnd(',');
        }
    
        @* Tell umbraco to publish the document *@
    
                doc.Publish(author);
                umbraco.library.UpdateDocumentCache(doc.Id);
    
                successfulPost = true;
            }
        }
    }
    
        @* Render the Html *@
        <hr />
        @if (successfulPost)
        {
            <h3>Successful post</h3>
            <p>Thank you <strong>@name</strong> for your message:</p>
            <p>@message</p>
        }
    
    
        <h3>Add a new Deal:</h3>
    
        <form method="post" enctype = "multipart/form-data">
            <fieldset>
                <label for="name">Tagline</label><br />
                <input type="text" name="name" id="name" /><br />
                <label for="price">Deal Price</label><br />
                <input type="text" name="price" id="price" /><br />
                <label for="parent">Location</label><br />
                <select name="parent" id="parent">
                    <option value="1065">Spain</option>
                    <option value="1117">Portugal</option>
                    <option value="1121">Italy</option>
                </select><br />
                <label for="message">Details</label><br />
                <textarea name="message" id="message"></textarea><br />
                <label for="price">Image</label><br />
                <input type="file" name="image" id="image" multiple/><br />
                <input type="submit" value="Post comment" />
            </fieldset>
        </form>
    
Please Sign in or register to post replies

Write your reply to:

Draft