This is a migrated thread and some comments may be shown as answers.

Handling DateTime as a custom result property - DON'T!

0 Answers 41 Views
AsyncUpload
This is a migrated thread and some comments may be shown as answers.
Allen
Top achievements
Rank 2
Iron
Veteran
Allen asked on 01 Aug 2020, 02:22 PM

I ran across a minor but annoying problem with sending a time back from the handler through a custom result.  Using datatype DateTime will not work.  The value comes through from the server as a long timestamp, which is wonderful, but that does not parse to a DateTime directly. It has to be a string-ified.  However, this cannot be done in the OnFileUploaded event because by then the server has already choked on converting Date(long value) to a DateTime.  The solution is to use a timestamp represented as a long datatype  for the result property, send that across , and decode / parse on the other end (client side) as desired. like this:

In the custom result:

        /// instant of file upload
        /// this is a timestamp
        public long UploadTimeStampUTC
        {
            get;
            set;
        } = new DateTimeOffset(System.DateTime.Now.ToUniversalTime()).ToUnixTimeMilliseconds(); // this is overkill

          public int UploadResultStatusCode { get; set; } // whatever code you want

        public string UploadResultMessage // happy.. or very, very  sad
        {
            get;
            set;
        } 

(not all properties shown....)

 

 

In the handler
...

..

 var tsLong = new DateTimeOffset(System.DateTime.Now.ToUniversalTime()).ToUnixTimeMilliseconds();
                    result.UploadTimeStampUTC = tsLong;

result.myUploadTime = tsLong ;

....

...

 result.UploadResultMessage = $"Status Code: {result.UploadResultStatusCode} - Process Result Message: {logMsg} {Environment.NewLine}" +
                        $"Original FileName: {file.FileName}{Environment.NewLine}" +
                        $"TempUploadFileName {FullPath.Replace(System.IO.Path.DirectorySeparatorChar, System.IO.Path.AltDirectorySeparatorChar)}{Environment.NewLine}" +
                        $"UploadResultFileName: {result.UploadResultFileName}{Environment.NewLine}" +
                        $"UploadResultPhysicalFileName: {result.PhysicalFileName}{Environment.NewLine}" +
                        $"UploadDateTime: {(Helpers.UnixTimeStampToDateTime(tsLong).ToString("F", CultureInfo.CreateSpecificCulture("en-US")))}";

return result;

...

 

In OnFileUploaded:

...

...

 LBISAsyncUploadResultError result = e.UploadResult as LBISAsyncUploadResultError;
            Log?.Debug(result.UploadResultMessage);
            Log?.Debug($"Filename on disk: {result.PhysicalFileName}");
            Log?.Debug($"TITLE: {e.File.GetFieldValue("Title")}");
            Log?.Debug($"NOTES: {e.File.GetFieldValue("Notes") }");
            Log?.Debug($"TIMESTAMP (as long): {result.UploadTimeStampUTC.ToString()}");
            Log?.Debug($"DATETIME: {(Helpers.UnixTimeStampToDateTime(result.UploadTimeStampUTC).ToString("F", CultureInfo.CreateSpecificCulture("en-US")))}");

 

the results look like:

...

...

2020-08-01 10:12:53,642 [13] DEBUG LOGGER [] - Status Code: 100 - Process Result Message: Uploaded successfully 
Original FileName: BrM6 - Troubleshooting-memo.pdf
TempUploadFileName D:/git/LBIS9/src/SupportDocuments/_tmp_RadUpload/1596291166563BrM6 - Troubleshooting-memo.pdf.tmp
UploadResultFileName: BrM6 - Troubleshooting-memo_[60051].pdf
UploadResultPhysicalFileName: D:/git/LBIS9/src/SupportDocuments/ExstPlans/BrM6 - Troubleshooting-memo_[60051].pdf
UploadDateTime: Saturday, August 1, 2020 14:12:48
2020-08-01 10:12:53,642 [13] DEBUG LOGGER [] - Filename on disk: D:/git/LBIS9/src/SupportDocuments/ExstPlans/BrM6 - Troubleshooting-memo_[60051].pdf
2020-08-01 10:12:53,642 [13] DEBUG LOGGER [] - TITLE: aaa
2020-08-01 10:12:53,643 [13] DEBUG LOGGER [] - NOTES: BBB
2020-08-01 10:12:53,643 [13] DEBUG LOGGER [] - TIMESTAMP (as long): 1596291168252
2020-08-01 10:12:53,643 [13] DEBUG LOGGER [] - DATETIME: Saturday, August 1, 2020 14:12:48

...

 

and this.... for which I cannot claim any credit

            /// <summary>
        /// Take a long that is a timestamp and make a datetime
        /// </summary>
        /// <param name="unixTimeStamp"></param>
        /// <returns></returns>
        public static DateTime UnixTimeStampToDateTime(long unixTimeStamp)
        {
            var UnixEpoch = new DateTime(1970, 1, 1, 0, 0, 0, 0, System.DateTimeKind.Utc);
 
            UnixEpoch = UnixEpoch.AddMilliseconds(unixTimeStamp);
            return UnixEpoch;
        }
        /// <summary>
        /// Take a DateTimeOffset and return a DateTime
        /// </summary>
        /// <param name="ts"></param>
        /// <returns></returns>
        public static DateTime DateTimeOffSetToDateTime(DateTimeOffset ts)
        {
            return DateTime.Parse(ts.ToString());
        }

 

No answers yet. Maybe you can help?

Tags
AsyncUpload
Asked by
Allen
Top achievements
Rank 2
Iron
Veteran
Share this question
or