天天看點

.net MVC 項目中 上傳或者處理進度擷取方案

首先講下思路

就是利用js輪詢定時的給背景發送資料

話不多說看代碼

.net MVC 項目中 上傳或者處理進度擷取方案
.net MVC 項目中 上傳或者處理進度擷取方案
.net MVC 項目中 上傳或者處理進度擷取方案
.net MVC 項目中 上傳或者處理進度擷取方案

---------

以下是相關方法

var t
    function timedCount() {
        $.ajax({
            type: 'get',
            url: '../../TMS.Service/OrderImport/GetImportProcess?cacheKey=' + cacheKey,
            dataType: 'json',
            success: function (data) {
                if (data.status) {
                    //讀取一次銷毀一次
                    $('#redMsg').html("<label>目前進度:</label><span style='color:red;font-size: large;'>" + data.msg + "</span>");
                }
            },
        });
        t = setTimeout("timedCount()", 2000)
    }      

 cacheKey = guid();

var cacheKey = "";

function guid() {

function S4() {

return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);

}

return (S4() + S4() + "-" + S4() + "-" + S4() + "-" + S4() + "-" + S4() + S4() + S4());

}

------接下來 看下背景代碼

/// <summary>
        ///擷取執行進度查詢
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult GetImportProcess(string cacheKey=null)
        {
            try
            {

                var result = new OrderImportBiz().GetImportProcess(cacheKey);
                return new ReponseModel { status = !string.IsNullOrEmpty(result), msg = result };
            }
            catch (Exception ex)
            {

                return new ReponseModel { status = false, msg = $"<br>擷取進度失敗了{ex.Message}!</br>" };
            }
        }      
CacheHelper.SetCache(MSG_CACHE_KEY, "正在準備向系統申請添加資料!");      
/// <summary>
    /// 緩存幫助類
    /// </summary>
    public class CacheHelper
    {
        /// <summary>
        /// 擷取資料緩存
        /// </summary>
        /// <param name="cacheKey">鍵</param>
        public static object GetCache(string cacheKey)
        {
            var objCache = HttpRuntime.Cache.Get(cacheKey);
            return objCache;
        }
        /// <summary>
        /// 設定資料緩存
        /// </summary>
        public static void SetCache(string cacheKey, object objObject)
        {
            var objCache = HttpRuntime.Cache;
            if (objCache!=null && objObject!=null)
            {
                objCache.Insert(cacheKey, objObject);
            }
            
        }
       
        /// <summary>
        /// 
        /// </summary>
        /// <param name="cacheKey"></param>
        /// <param name="objObject"></param>
        /// <param name="timeout">機關秒 預設7200秒</param>
        public static void SetCache(string cacheKey, object objObject, int timeout = 2)
        {
            try
            {
                if (objObject == null) return;
                var objCache = HttpRuntime.Cache;
                //相對過期
                //objCache.Insert(cacheKey, objObject, null, DateTime.MaxValue, timeout, CacheItemPriority.NotRemovable, null);
                //絕對過期時間
                objCache.Insert(cacheKey, objObject, null, DateTime.Now.AddSeconds(timeout), TimeSpan.Zero, CacheItemPriority.High, null);
            }
            catch (Exception)
            {
                //throw;
            }
        }
        /// <summary>
        /// 移除指定資料緩存
        /// </summary>
        public static void RemoveAllCache(string cacheKey)
        {
            var cache = HttpRuntime.Cache;
            cache.Remove(cacheKey);
        }
        /// <summary>
        /// 移除全部緩存
        /// </summary>
        public static void RemoveAllCache()
        {
            var cache = HttpRuntime.Cache;
            var cacheEnum = cache.GetEnumerator();
            while (cacheEnum.MoveNext())
            {
                cache.Remove(cacheEnum.Key.ToString());
            }
        }
    }