Retrieving file name of uploaded files
Retrieving file name of uploaded files

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
Fixed: I can pick up the names of files via preUpload event.
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:
The var. strCdrFileNme1 never gets set w/ the file name. Please advise.
I think you are probably best to use the
ajaxData
option of theupload
field type here. That allows you to modify the Ajax data specifically for the upload action (since it uses aFormData
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:
to be:
it will give you access to the file object in the ajaxData callback. I'll have that in for v1.9.1
Regards,
Allan
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:
ajaxData
is a function that is passed in (currently) theFormData
object so you can modify it: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:will add the file name to the data sent to the server. See the MDN documentation for the Javascript
File
object's API.Allan
Thank you.