Retrieving file name of uploaded files

Retrieving file name of uploaded files

dynasoftdynasoft Posts: 422Questions: 67Answers: 3

Hi

I need to retrieve the name and path of files being uploaded in order to set fields in my server code. Eg, in server code below I need to return the creation date of the file being uploaded and set this in a table field (called DateFile). How can I extract the file path and name? Can this be done via the 'd' object in the js code? If so what is the property for it. I'm including the Js code below.

Thanks.

  • Server code:

public ActionResult CRUDCDRDataAndFiles(int intCdrFrmtSrc, int intCdrFrmtTpe, string strCdrFrmtNme)
{
Editor editor = null;

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("AutoCDRFiles.DateFile")
        .GetFormatter((val, data) => CommonUtilities.ToDateTimeString(CommonUtilities.ToDateTime(val), 1))
        .SetValue(CommonUtilities.GetFileCreateDate(CommonUtilities.ToString(FILEPATHNAME), string.Empty, string.Empty))
    );        
    editor.MJoin(new MJoin("UserFiles")
        .Link("UserFiles.id", "AutoCDRFiles.UserFileID")
        .Field(
            new Field("id")
                .SetFormatter(Format.IfEmpty(0))
                .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}
                })
            )
        )
    );
    editor.TryCatch(false);
    editor.Debug(true);
    editor.Process(formData);
    editor.Data();
}

return Json(editor.Data(), JsonRequestBehavior.AllowGet);           

}

  • Js code:
    ajax: {
    url: '/CDRData/CRUDCDRDataAndFiles/',
    data: function ( d ) {
    return $.extend( {}, d, {
    intCdrFrmtSrc: intCdrFrmtSrc1,
    intCdrFrmtTpe: intCdrFrmtTpe1,
    strCdrFrmtNme: strCdrFrmtNme1,
    strEditorState: strEditorState1
    } );
    }
    },

Answers

  • dynasoftdynasoft Posts: 422Questions: 67Answers: 3

    Fixed: I can pick up the names of files via preUpload event.

  • dynasoftdynasoft Posts: 422Questions: 67Answers: 3

    does not work as preUpload is called after js ajax coe above gets called but the filename is set in preUpload and so if I change the js code and add a new argument for the file name like so:

                    data: function (d) {
                        //alert(d.file);
                        return $.extend( {}, d, {
                            intCdrFrmtSrc: intCdrFrmtSrc1,
                            intCdrFrmtTpe: intCdrFrmtTpe1,
                            strCdrFrmtNme: strCdrFrmtNme1,
                            strCdrFileNme: strCdrFileNme1,
                            strEditorState: strEditorState1
                        } );
                    },
    

    The var. strCdrFileNme1 never gets set w/ the file name. Please advise.

  • allanallan Posts: 61,446Questions: 1Answers: 10,054 Site admin

    I think you are probably best to use the ajaxData option of the upload field type here. That allows you to modify the Ajax data specifically for the upload action (since it uses a FormData object).

    I was just looking at the code for this and saw that it doesn't actually pass in the file itself, which was a bit daft of me.

    If you change:

    conf.ajaxData( data );
    

    to be:

    conf.ajaxData( data, files[ counter ], counter );
    

    it will give you access to the file object in the ajaxData callback. I'll have that in for v1.9.1

    Regards,
    Allan

  • dynasoftdynasoft Posts: 422Questions: 67Answers: 3

    Sorry but I don't see how ajaxData is used. There is very little on your doc about it.

    Here's my uploadMany field type:

                        label: '@(lblo.lblUploadedFile)*:',
                        name: 'AutoCDRFiles[].UserFileID',
                        type: 'uploadMany',
                        ajaxData: strCdrFileNme1,
                        display: function (fileId, counter) {
                            return fileId ? GetEditorUploadFileRender(editor1.file( 'UserFiles', fileId ).WebPath, editor1.file( 'UserFiles', fileId ).FileName, 1, '@strFileIconWebPath') : null;
                        }
    
  • allanallan Posts: 61,446Questions: 1Answers: 10,054 Site admin

    ajaxData is a function that is passed in (currently) the FormData object so you can modify it:

    ajaxData: function (data) {
      data.append('myVariable', 'myValue');
    }
    

    The change I suggested above (and will be in 1.9.1) is to also pass in the File object and the file counter (for when there are multiple files being uploaded at the same time). So:

    ajaxData: function (data, file) {
      data.append('fileName', file.name);
    }
    

    will add the file name to the data sent to the server. See the MDN documentation for the Javascript File object's API.

    Allan

  • dynasoftdynasoft Posts: 422Questions: 67Answers: 3

    Thank you.

This discussion has been closed.