Editor File upload

Editor File upload

klymov.inekonklymov.inekon Posts: 41Questions: 12Answers: 0

Hi, I have a question about file uploading.

I tried to download the .exe file (such requirements). During a example upload, it caused the page to refresh. I got around this by adding it to the ajax field.

{
    name: 'File',
    data: 'File',
    label: 'File',
...
    type: 'upload',
    attr: {
        accept: ".exe,.msi",
    },

    ajax: function (files, done) {
        if (files.length == 0) return;
        done(files)
    },

    display: function (file) {
        return `<span> ${file.name} </span>`;
    },

},

It worked, and when I submit, I see in the data that the field has a value with the type FILE.


create: { type: 'POST', url: 'route', data: function (d) { debugger var file = d.data[0].ClientVersion.File; // file exist return d; }, },

But then I've an errors

jquery.js:10073  Uncaught TypeError: Illegal invocation
jquery.js:10073  Uncaught (in promise) TypeError: Failed to execute 'arrayBuffer' on 'Blob': Illegal invocation

I've tried to set contentType but it not helped.

    contentType: 'application/x-www-form-urlencoded',

What should I do to send it to .net editor endpoint?

I don't need this file saved on server some way. I need to store it to database via MemoryStream. I've set for now in endpoint

.SetFormatter((val,data)=>{
    var file = val as IFormFile;
    if(file is null) return null;
    using (var memoryStream = new MemoryStream())
    {
        file.CopyTo(memoryStream);
        var fileContent = memoryStream.ToArray();
        return fileContent;
    }
}),

Maybe some solution to do this in js before sending file, rewrite field into byte array and send like standard value?

Answers

  • allanallan Posts: 65,623Questions: 1Answers: 10,910 Site admin

    I tried to download the .exe file

    Do you mean you are trying to upload an exe via Editor? I don't provide a Windows executable for any DataTables software. There is a .NET demo package for Editor which includes a DLL though.

    What should I do to send it to .net editor endpoint?

    You shouldn't need to do anything other than set the ajax url. The demo should accept files and the server will then process them (and in this case reject an exe). Is that not happening for you in that example?

    Allan

  • allanallan Posts: 65,623Questions: 1Answers: 10,910 Site admin

    Also, for a custom upload action, check out the documentation here. A set formatter isn't the right place to handle the file.

    Allan

  • klymov.inekonklymov.inekon Posts: 41Questions: 12Answers: 0

    Okay, maybe there is a solution to transfer this file in byte[] format. Because I need this file in the database in exactly this format. I don't need to upload the entire file to the server in a separate location.

  • allanallan Posts: 65,623Questions: 1Answers: 10,910 Site admin

    You mean you want to store the file in the database? Have a look at Upload.DbType.Content in the upload documentation. That can be used to write the content of the file to a db.

    The downside of course is that you need to query the DB to get the file. That might be okay in some cases, but if you are working with images embedded in a page, there is a performance penalty. Apologies if you know this already, but it might be useful for someone else reading.

    Allan

Sign In or Register to comment.