Errors with uploadMany

Errors with uploadMany

dynasoftdynasoft Posts: 422Questions: 67Answers: 3

Hi

Errors are cropping up when loading the page and the datatable and when trying to upload a file (after I comment out the part where d.length is mentioned):

My code:

  • JS:

        var editor1 = new $.fn.dataTable.Editor({
    
            ajax: {
                url: '/CDRData/CRUDCDRDataAndFiles/',
                data: {
                    intCdrFrmtSrc: intCdrFrmtSrc1,
                    intCdrFrmtTpe: intCdrFrmtTpe1,
                    strCdrFrmtNme: strCdrFrmtNme1
                },
                type: 'POST',
                async: true,
                cache: false
            },
            table: '#tblCDRDataAndFilesTable',
            fields: [ {
                    label: '@(lblo.lblUploadedFile)*:',
                    name: 'UserFiles[].UserFileID',
                    type: 'uploadMany',
                    display: function ( id, counter ) {
                        return id ? "SomeLink" : null;
                    },
                    clearText: '@(lblo.lblClear)',
                    noFileText: '@(lblo.lblNoFile)'
                }
            ],
            i18n: {
                error: {
                    system: '@(lblo.lblError5)'
                }
            }
        });
    
        var dataTable = $('#tblCDRDataAndFilesTable').DataTable( {
    
            destroy: true,
            order: [[0, 'desc']],
            columnDefs: [
                { 'bVisible': false, 'targets': 0 },
                { targets: [0,1],
                    className: 'text-center'
                }
            ],
            dom: 'Bfrtip',
            ajax: {
                url: '/CDRData/CRUDCDRDataAndFiles/',
                data: {
                    intCdrFrmtSrc: intCdrFrmtSrc1,
                    intCdrFrmtTpe: intCdrFrmtTpe1,
                    strCdrFrmtNme: strCdrFrmtNme1
                },
                type: 'GET',
                dataType: 'json',
                contentType: 'application/json; charset=utf-8',
                async: true
            },
            columns: [
                { data: 'id' ,
                    className: 'text-left'
                },
                { data: "UserFiles",                        //*HERE*
                    render: function ( d ) {
                        return d.length ?
                            d.length+' image(s)' :
                            '@(lblo.lblNoFile)';
                    },
                    className: 'text-left',
                    defaultContent: '@(lblo.lblNoFile)',
                    title: '@(lblo.lblFile)'
                }
            ],
            select: true,
            buttons: [
                { extend: 'create', editor: editor1 },
                { extend: 'edit', editor: editor2 },
                { extend: 'remove', editor: editor2 }
            ]
        });
    
  • Server:

    public static DtResponse CRUDCDRDataAndFiles(int intCdrFrmtSrc, int intCdrFrmtTpe, string strCdrFrmtNme)
    {
        Editor editor = null;
    
        try
        {
            HttpRequest formData = System.Web.HttpContext.Current.Request;
    
            using (Database db = new Database(SetGetDbType2, SetGetDbConnection))
            {
                editor = new Editor(db, "AutoCDRFiles").Model<CDRDataDBModel>();
                editor.Field(new Field("id")
                    .Set(false)
                );
                editor.Field(new Field("UserFileID")
                    .SetFormatter(Format.IfEmpty(null))
                    .Upload(new Upload(strFolder + @"\__NAME____EXTN__")
                        .Db("UserFiles", "id", new Dictionary<string, object>
                        {
                            {"WebPath", Upload.DbType.WebPath},
                            {"SystemPath", Upload.DbType.SystemPath},
                            {"FileName", Upload.DbType.FileName},
                            {"FileSize", Upload.DbType.FileSize}
                        })
                    )
                );
                if (intCdrFrmtSrc > -1) editor.Where("CDRSourceType", intCdrFrmtSrc);
                if (intCdrFrmtTpe > -1) editor.Where("CDRDataType", intCdrFrmtTpe);
                if (intCdrFrmtTpe > -1) editor.Where("CDRName", strCdrFrmtNme);
                editor.TryCatch(false);
                editor.Debug(true);
                editor.Process(formData);
                editor.Data();
            }                
        return editor.Data();
    }
    

Many thanks

Answers

  • dynasoftdynasoft Posts: 422Questions: 67Answers: 3

    Hi

    For error #1, I had to use UserFileID as the column reference to fix it:

                    { data: "UserFileID",
                        render: function ( d ) {
                            return d.length ?
                                d.length+' image(s)' :
                                '@(lblo.lblNoFile)';
                        },
                        className: 'text-left',
                        defaultContent: '@(lblo.lblNoFile)',
                        title: '@(lblo.lblFile)'
                    }
    

    For the 2nd error if I use this:

    ajax: {
        url: '/CDRData/CRUDCDRDataAndFiles/',
        data: {
            intCdrFrmtSrc: intCdrFrmtSrc1,
            intCdrFrmtTpe: intCdrFrmtTpe1,
            strCdrFrmtNme: strCdrFrmtNme1
        },
        type: 'POST',
        async: true,
        cache: false
    }
    

    I'm able to create and update all the fields except for the upload one. What is the correct syntax to use to work with "standard2 fields and uploads fields? Thank you.

  • dynasoftdynasoft Posts: 422Questions: 67Answers: 3

    with "standard" fields and uploads fields

  • dynasoftdynasoft Posts: 422Questions: 67Answers: 3

    Hi

    I'd like to keep using parameters if I can as above.

  • allanallan Posts: 61,450Questions: 1Answers: 10,055 Site admin

    standard fields and uploads fields

    I'm afraid I don't quite understand what you are looking for. Could you elaborate on this a little more for me please?

    Thanks,
    Allan

  • dynasoftdynasoft Posts: 422Questions: 67Answers: 3

    Hi

    I'd like to use ajax parameters like this when working with upload functionality:

    ajax: {
    url: '/CDRData/CRUDCDRDataAndFiles/',
    data: {
    intCdrFrmtSrc: intCdrFrmtSrc1,
    intCdrFrmtTpe: intCdrFrmtTpe1,
    strCdrFrmtNme: strCdrFrmtNme1
    },
    type: 'POST',
    async: true,
    cache: false
    }

    But I get a (Internal Server Error). I don't get this error if I do not use any upload code in my page, ie work with json data. The only format that will work is working w/ uploads is this:

                ajax: { //works
                    url: '/CDRData/CRUDCDRDataAndFiles/' + intCdrFrmtSrc1.toString() + '/' + intCdrFrmtTpe1.toString() + '/' + strCdrFrmtNme1,
                }
    

    But under MVC, I then have to fiddle round with routes which I'd like to avoid.

  • dynasoftdynasoft Posts: 422Questions: 67Answers: 3

    The only format that will work is working w/ uploads is this:

    should read:

    The only format that will work working w/ uploads is this:

  • allanallan Posts: 61,450Questions: 1Answers: 10,055 Site admin

    The upload field type (and uploadMany) has a ajaxData option which can be used to send extra information to the server - e.g.:

    ajaxData: function (f) {
      f.append( intCdrFrmtSrc, intCdrFrmtSrc1 );
    }
    

    Allan

  • dynasoftdynasoft Posts: 422Questions: 67Answers: 3

    Hi

    Thanks. Should I replace this:

    ajax: { //works
    url: '/CDRData/CRUDCDRDataAndFiles/' + intCdrFrmtSrc1.toString() + '/' + intCdrFrmtTpe1.toString() + '/' + strCdrFrmtNme1,
    }

    with this:

                ajax: {
                    url: '/CDRData/CRUDCDRDataAndFiles/',
                },
                ajaxData: function (f) {
                    f.append( intCdrFrmtSrc1, intCdrFrmtTpe1, strCdrFrmtNme1 );
                },
    

    I tried but get a 500 error.

  • dynasoftdynasoft Posts: 422Questions: 67Answers: 3

    The controller action is this one:

        public ActionResult CRUDCDRDataAndFiles(int intCdrFrmtSrc, int intCdrFrmtTpe, string strCdrFrmtNme)
        {
                return Json(ContactNotesModel.CRUDCDRDataAndFiles(intCdrFrmtSrc, intCdrFrmtTpe, strCdrFrmtNme), JsonRequestBehavior.AllowGet);
        }
    
  • dynasoftdynasoft Posts: 422Questions: 67Answers: 3

    Hi

    Have been trying a few things and found the following works well:

                ajax: {
                    url: '/ContactNotes/CRUDCDRDataAndFiles/',
                    data: function ( d ) {
                        return $.extend( {}, d, {
                            intCdrFrmtSrc: intCdrFrmtSrc1,
                            intCdrFrmtTpe: intCdrFrmtTpe1,
                            strCdrFrmtNme: strCdrFrmtNme1
                        } );
                    }
                },
    
This discussion has been closed.