2sic / app-mobius-forms

Simple jQuery based 2sxc form - to use immediately or to modify as needed. Multi-language, simple, with Recaptcha

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[Documentation request]Isolated file upload to adam features

enfJoao opened this issue · comments

I've read quite a few posts about file upload, and tested the moebious forms app, but I can't quite figure how file upload works.

  1. What is the relation between the adam folder name and the entity guid/id?

  2. Could you help create a simple app demo that demonstrates the single file upload feature? For us newbs it's way easier to integrate features without complex environments working around it.

Here's a headstart:

view:

<div>
    <label for="File">Text</label>
    <div>
        <input type="text" id="testText">
    </div>
</div>

<div>
    <label for="File">File (jpeg only)</label>
    <div>
        <input type="file" id="SingleFile" accept=".jpg, .jpeg" value="">
    </div>
</div>

<div>
    <label for="File">Any file</label>
    <div>
        <input type="file" name="Attachments" id="File2_1" value="">
    </div>
</div>

<div>
    <label for="File">Another file</label>
    <div>
        <input type="file" name="Attachments" id="File2_2" value="">
    </div>
</div>

<div>
    <button id="sendData" type="button">SendThis</button>
</div>

<script>
$(document).on("click", "#sendData", sendData);

function getData() {
    return {
        text: $( '#testText' ).val(),
        Files: [],
        user: "@Dnn.User.Username"
    };
}

function sendData() {
    var newItem = getData();
    
    // push file data here
    
    $2sxc(@Dnn.Module.ModuleID).webApi.post("file/ProcessForm", {}, newItem, true);
}
</script>

controller:

using DotNetNuke.Security;
using DotNetNuke.Web.Api;
using System.Web.Http;
using ToSic.SexyContent.WebApi;
using System.Collections.Generic;
using System;
using System.Linq;
using System.Web.Compilation;
using System.Runtime.CompilerServices;
using DotNetNuke.Services.Mail;
using Newtonsoft.Json;
using System.IO;

public class fileController : SxcApiController
{
    [HttpPost]
    [DnnModuleAuthorize(AccessLevel = SecurityAccessLevel.Anonymous)]
    [ValidateAntiForgeryToken]
    public void ProcessForm([FromBody]Dictionary<string,object> contactFormRequest)
    {
        contactFormRequest = new Dictionary<string, object>(contactFormRequest, StringComparer.OrdinalIgnoreCase);

        var guid = Guid.NewGuid();
        contactFormRequest.Add("EntityGuid", guid);
        App.Data.Create("demoData", contactFormRequest);

        var files = new List<ToSic.Sxc.Adam.IFile>();
        foreach(var file in ((Newtonsoft.Json.Linq.JArray)contactFormRequest["Files"]).ToObject<IEnumerable<Dictionary<string, string>>>())
        {
            var data = Convert.FromBase64String((file["Encoded"]).Split(',')[1]);
            files.Add(SaveInAdam(stream: new MemoryStream(data), fileName: file["Name"], contentType: "demoData", guid: guid, field: file["Field"]));
        }
    }
}

I like the idea, i'll see where this could be integrated. It will take ca. 2-3 weeks though, because I'm currently swamped with other priorities.

Thanks.