File upload: How to retrieve row id?

File upload: How to retrieve row id?

nchong128nchong128 Posts: 3Questions: 1Answers: 0

Hi there,

I am trying to give the option of uploading a file for a given row. So far I've added an upload field in the Editor that will send an AJAX to send a post request to my back-end. However, I am having trouble retrieving the row ID or anything identifier for the row.

I appreciate your help

This question has an accepted answers - jump to answer

Answers

  • colincolin Posts: 15,142Questions: 1Answers: 2,586

    Hi @nchong128 ,

    I'm not too clear about your question, but this example here shows how to implement file upload. If that doesn't resolve your issue, please could you post your code and explain where you're trying to get the row ID.

    Cheers,

    Colin

  • nchong128nchong128 Posts: 3Questions: 1Answers: 0

    Hi @colin ,

    My apologies for not being descriptive enough. Put simply, I am trying to give my client the option to upload file(s) for each row in the table.

    To implement this, I have added a field in the edit box which would send a POST request to my Express server at the url /overview/file/{the table's id}.

                        {
                            label: "Additional Delivery Info",
                            name: "deliveryInfoFiles",
                            type: "uploadMany",
                            display: function (fileId) {
                                return editor.file( 'files',fileId).fileName;
                            },
                            attr: {
                                placeholder: "Not set"
                            },
                            limit: 2,
                            ajax: {
                                type: 'POST',
                                url: `/overview/file/<%-id%>`,
                                contentType: 'application/json',
                                data: (d) => {
                                    console.log(d);
                                    return JSON.stringify(d);
                                }
                            }
                        }
    

    The file is received at the back-end successfully with something like this.
    //rest of JSON
    files:
    { upload:
    { name: 'secret.txt',
    data: <Buffer 62 61 72 6b 6f 69 73 6c 6f 76 65 31 0d 0a 42 61 72 6b 6f 49 73 4c 6f 76 65 24 31>,
    size: 27,
    encoding: '7bit',
    tempFilePath: '',
    truncated: false,
    mimetype: 'text/plain',
    md5: 'e8137e0f5cfe28470a8bd5752a832295',
    mv: [Function: mv] } },
    //rest of JSON

    However, I am having trouble finding which row the files belong to, as it doesn't seem to be in the POST request. Updating info based on rows works fine as the info is given in key/value pairs with the key being the row's unique id, but it doesn't seem to be the case for uploading files.

    For the record, I am just using Datatable's Javascript version (not PHP or .NET) so I am reluctant to use the Upload class unless it really is required to implement this feature.

    Thank you

  • colincolin Posts: 15,142Questions: 1Answers: 2,586
    Answer ✓

    Hi @nchong128 ,

    It would be worth looking at that example I posted in my first reply, since I think that will do what you want - also, this example shows how to upload multiple files.

    If you want to stay with what you've got, you would add the row ID to data in the data function at line 16, this would then be readable on the server. Without seeing where that Ajax function is being called, it's hard to know where you would get it from. If it's from Editor's edit form, you would use ids(), otherwise, it's hard to tell without seeing the bigger picture.

    I would definitely look at Editor's upload examples though, I think they'll work for you,

    Cheers,

    Colin

  • nchong128nchong128 Posts: 3Questions: 1Answers: 0

    ids() was well needed. Thank you very much :)

This discussion has been closed.