Skip to content

[Documentation request]Isolated file upload to adam features #19

@enfJoao

Description

@enfJoao

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"]));
        }
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions