Retrieving file name of uploaded files

Retrieving file name of uploaded files

dynasoftdynasoft Posts: 166Questions: 30Answers: 2

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: 166Questions: 30Answers: 2

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

  • dynasoftdynasoft Posts: 166Questions: 30Answers: 2

    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: 50,248Questions: 1Answers: 7,465 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

Sign In or Register to comment.