Copied to clipboard

Flag this post as spam?

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


  • Christian Hansen 34 posts 204 karma points
    Aug 06, 2019 @ 18:25
    Christian Hansen
    0

    Dont run code if no media in file picker

    Hello, i am using the content and media service, to create content and upload media.

    All works fine. But when i dont pick a media/file it still goes in my foreach var file in model.uploadFiles.

    my form view.

    @using (Html.BeginUmbracoForm<Sportscloud.dk.Controllers.AthletePostController>("HandleFormSubmit"))
    {
    <div class="form">
        <div class="col-xs-12">
            @Html.TextBoxFor(m => m.PostName, new { @class = "form-control", placeholder = "Emne" })
            @Html.ValidationMessageFor(m => m.PostName)
        </div>
        <div class="col-xs-12">
            @Html.TextAreaFor(m => m.Text, new { @class = "form-control", placeholder = "Tekst" })
            @Html.ValidationMessageFor(m => m.Text)
        </div>
        <div>
            <input id="btnUploadFiles" type="file" name="UploadFiles" multiple/>
            @Html.ValidationMessageFor(m => m.UploadFiles)
        </div>
        <div class="createPost">
            <button id="contact-submit" type="submit" class="btn btn-primary input-group-sm">Opret</button>
        </div>
    </div>  
    }
    

    My model:

    [Required]
        public string PostName { get; set; }
        [Required]
        public string Text { get; set; }
    
        public IEnumerable<HttpPostedFileBase> UploadFiles { get; set; }
    

    My controller:

    if (!ModelState.IsValid)
                return CurrentUmbracoPage();
    
            //Create content
            var newRecord = Services.ContentService.CreateContent(model.PostName, CurrentPage.Id, "federationRecordPage");
            newRecord.SetValue("header", model.PostName);
            newRecord.SetValue("text", model.Text);
    
            //create new media files
            var gallerySavedPics = "";
    
            foreach (var file in model.UploadFiles)
            {
    
                var FileType = "";
                if (file.FileName.Contains(".pdf"))
                {
                    FileType = "File";
                }
                else
                {
                    FileType = "Image";
                }
    
                int MemberMediaFolder = Convert.ToInt32(CurrentPage.GetProperty("athleteMediaFolder").Value.ToString());
    
                IMedia uploadMediaFiles = Services.MediaService.CreateMedia(file.FileName, MemberMediaFolder, FileType);
                uploadMediaFiles.SetValue("umbracoFile", file);
                Services.MediaService.Save(uploadMediaFiles);
    
                gallerySavedPics += uploadMediaFiles.GetUdi().ToString() + ",";
    
    
            }
    
            gallerySavedPics = gallerySavedPics.Substring(0, gallerySavedPics.Length - 1);
    
            if (gallerySavedPics != "")
            {
    
                newRecord.SetValue("documentUpload", gallerySavedPics);
            }
    
            Services.ContentService.SaveAndPublishWithStatus(newRecord);
    

    How can i avoid to run my foreach (var file in model.UploadFiles) if there is no files in my input file picker.

  • Marc Goodson 2155 posts 14408 karma points MVP 9x c-trib
    Aug 06, 2019 @ 20:47
    Marc Goodson
    100

    Hi Christian

    This is because in Input field of filetype, does post something, even if there is no file... the trick is to check ContentLength of the first element in the IEnumerable of HttpPostedFileBase, and only run your foreach loop is there is something there with a ContentLength...

    You could have an extension method like so:

    public static bool HasFiles(this IEnumerable<HttpPostedFileBase> files)
        {
            return files.First() != null && files.First().ContentLength > 0;
        }
    

    and then that would enable you to write:

    if (model.UploadFiles.HasFiles()){
    
     foreach (var file in model.UploadFiles)
            {
    
                var FileType = "";
    

    ...

    regards

    Marc

  • Christian Hansen 34 posts 204 karma points
    Aug 07, 2019 @ 07:20
    Christian Hansen
    0

    Hey Marc,

    Thanks! That worked great!

Please Sign in or register to post replies

Write your reply to:

Draft