Having an issue returning file uploads

Having an issue returning file uploads

nuggetap3nuggetap3 Posts: 24Questions: 5Answers: 2

When I attach a file to on of the rows in my table it returns a link to the item. If it is a pdf, it returns a straight hyperlink. If it is an image type, it returns a link in the form of a thumbnail. However, I am having an issue when there is no attachment for that row. If there is no attachment, then it should simply say no file. However, when the row is created it is not returned to the datatable.
Here is my code

"data": "attachments",
                render: function (Id) {
                    var ext = traveleditor.file('expenseAttachment', Id).webPath.toLowerCase();
                    var ext1 = ext.split(".").pop();

                    if (ext1 == "pdf") {
                        //return '<center> <i class ="fas fa-file-pdf" style="font-size: 2.73em;"' + traveleditor.file('expenseAttachment', Id).webPath + '"</i></center>';
                        return '<a href ="' + traveleditor.file('expenseAttachment', Id).webPath + '" target=_blank><i class =" glyphicon glyphicon-file" style="font-size: 1em; "</i>' + traveleditor.file('expenseAttachment', Id).filename + '</center></a>';
                    }
                    else if ((ext1 == "png") || (ext1 == "jpg") || (ext1 == "gif")) {

                        return '<a href ="' + traveleditor.file('expenseAttachment', Id).webPath + '" target=_blank><img width="100px" src="' + traveleditor.file('expenseAttachment', Id).webPath + '"/></a>';
                    }
                    else {
                        return "No File";
                    }  

This question has accepted answers - jump to:

Answers

  • tangerinetangerine Posts: 3,365Questions: 39Answers: 395

    This may be a typo in your post, but your render() function is lacking a closing brace ( } ).

  • allanallan Posts: 63,455Questions: 1Answers: 10,465 Site admin
    Answer ✓

    I'd probably suggest having:

    if ( ! Id ) {
      return 'No File';
    }
    

    at the top of your function. At the moment the code is assuming that even if there is no id (which I don't think there will be if there is no file) it can still get information about that file (which doesn't exist).

    Allan

  • nuggetap3nuggetap3 Posts: 24Questions: 5Answers: 2
    Answer ✓

    Thanks Allan, we were able to get it working.

    ```
    "data": "attachments",
    render: function (Id) {

                        if (Id !== null) {
    
                            var ext = misceditor.file('expenseAttachment', Id).webPath.toLowerCase();
                            var ext1 = ext.split(".").pop();
    
                            if (ext1 == "pdf") {
    
                                 return '<a href ="' + misceditor.file('expenseAttachment', Id).webPath + '" target=_blank><i class =" glyphicon glyphicon-file" style="font-size: 1em; "</i>' + misceditor.file('expenseAttachment', Id).filename + '</center></a>';
    
    
                            }
    
                            if ((ext1 == "png") || (ext1 == "jpg") || (ext1 == "gif")) {
    
                                return '<a href ="' + misceditor.file('expenseAttachment', Id).webPath + '" target=_blank><img width="100px" src="' +misceditor.file('expenseAttachment', Id).webPath + '"/></a>';
    
                            }
                        }
                        else {
    
                            return "No File";
                        }
                    },
    
This discussion has been closed.