Esempio n. 1
0
 /// <summary> 添加文件日志
 /// </summary>
 /// <param name="item"></param>
 /// <param name="currAccDbName"></param>
 /// <returns></returns>
 public static bool Add(UserFileOPLog item, string currAccDbName)
 {
     Dictionary<string, string> DicParam = new Dictionary<string, string>();
     DicParam.Add("ClientUserID", item.userOP.UserID);
     DicParam.Add("OPName", item.userOP.OPName);
     DicParam.Add("LogID", System.Guid.NewGuid().ToString());
     DicParam.Add("FileName", item.FileName.Replace('\'', '"'));
     DicParam.Add("FilePath", item.FilePath.Replace('\'', '"'));
     DicParam.Add("sTimeTicks", item.sTimeTicks.ToString());
     DicParam.Add("eTimeTicks", item.eTimeTicks.ToString());
     DicParam.Add("exeResult", item.exeResult ? "1" : "0");
     return DBHelper.Submit_AddLog("添加文件日志", currAccDbName, "tl_ClientUserFile", "", "", DicParam);
 }
        /// <summary> 上传文件 
        /// </summary>
        /// <param name="request">1上传成功,0上传失败</param>
        public UploadFileResponse UploadFile(FileTransferMessage request)
        {
            UploadFileResponse response = new UploadFileResponse();
            response.Flag = false;
            UserFileOPLog uOpLog = new UserFileOPLog();
            UserIDOP uOp = new UserIDOP();
            uOp.UserID = request.UserID;
            uOp.OPName = "UploadFile";
            uOpLog.userOP = uOp;
            uOpLog.FileName = request.FileName;

            if (!Directory.Exists(GlobalStaticObj_Server.Instance.FilePath))//存放的默认文件夹是否存在
            {
                Directory.CreateDirectory(GlobalStaticObj_Server.Instance.FilePath);//不存在则创建
            }
            string path = GlobalStaticObj_Server.Instance.FilePath;
            if (!string.IsNullOrEmpty(request.SavePath))
            {
                path = Path.Combine(GlobalStaticObj_Server.Instance.FilePath, request.SavePath);
            }
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
            uOpLog.sTimeTicks = DateTime.UtcNow.Ticks;
            string fileName = request.FileName;//文件名
            string fileFullPath = Path.Combine(path, fileName);//合并路径生成文件存放路径
            uOpLog.FilePath = fileFullPath;
            Stream sourceStream = request.FileData;
            if (sourceStream == null) { return response; }
            if (!sourceStream.CanRead) { return response; }
            //创建文件流,读取流中的数据生成文件
            using (FileStream fs = new FileStream(fileFullPath, FileMode.Create, FileAccess.Write, FileShare.None))
            {
                try
                {
                    const int bufferLength = 4096;
                    byte[] myBuffer = new byte[bufferLength];//数据缓冲区
                    int count;
                    while ((count = sourceStream.Read(myBuffer, 0, bufferLength)) > 0)
                    {
                        fs.Write(myBuffer, 0, count);
                    }
                    fs.Close();
                    sourceStream.Close();
                    uOpLog.eTimeTicks = DateTime.UtcNow.Ticks;
                    uOpLog.exeResult = true;
                    response.Flag = true;
                    return response;
                }
                catch (Exception ex)
                {
                    uOpLog.eTimeTicks = DateTime.UtcNow.Ticks;
                    uOpLog.exeResult = false;
                    return response;
                    throw ex;
                }
                finally {
                    writeUserFileOpLog(uOpLog);
                }
            }
        }
        //private static string _savePath = null;
        //private static string SavePath
        //{
        //    get
        //    {
        //        if (_savePath == null)
        //        {
        //            lock (locker)
        //            {
        //                if (_savePath == null)
        //                {                            
        //                    _savePath = System.Configuration.ConfigurationManager.AppSettings["savePath"].ToString();
        //                }
        //            }
        //        }
        //        return _savePath;
        //    }
        //}

        private static void writeUserFileOpLog(UserFileOPLog uOpLog)
        {
            UserOPFileLogClass.Instance.Add(uOpLog);
        }
 /// <summary> 下载文件
 /// </summary>
 /// <param name="fileName">下载文件名称</param>
 /// <returns></returns>
 public Stream DownLoadFile(string fileName, string savePath, string userID, string cookieStr)
 {
     UserFileOPLog uOpLog = new UserFileOPLog();
     UserIDOP uOp = new UserIDOP();
     uOp.UserID = userID;
     uOp.OPName = "DownLoadFile";
     uOpLog.userOP = uOp;
     uOpLog.FileName = fileName;
     string fileFullPath = Path.Combine(GlobalStaticObj_Server.Instance.FilePath, fileName);//服务器文件路径
     if (!string.IsNullOrEmpty(savePath))
     {
         fileFullPath = Path.Combine(GlobalStaticObj_Server.Instance.FilePath, savePath, fileName);
     }
     uOpLog.FilePath = fileFullPath;
     if (!File.Exists(fileFullPath))//判断文件是否存在
     {
         return null;
     }
     FileStream myStream = null;
     try
     {
         uOpLog.sTimeTicks = DateTime.UtcNow.Ticks;
         myStream = File.OpenRead(fileFullPath);
        
     }
     catch (Exception ex) {
         uOpLog.eTimeTicks = DateTime.UtcNow.Ticks;
         uOpLog.exeResult = false;               
         return null;
         throw ex;
     }
     finally
     {
         uOpLog.eTimeTicks = DateTime.UtcNow.Ticks;
         uOpLog.exeResult = true;
         writeUserFileOpLog(uOpLog);
     }
     return myStream;
 }
 /// <summary> 下载文件
 /// </summary>
 /// <param name="fileName">下载文件名称</param>
 /// <returns></returns>
 public Stream DownLoadFile(string fileName, string savePath, string userID, string cookieStr)
 {
     //if (!HXCSession.SessionCookie.CheckPCClientCookieStr(userID,cookieStr))
     //{
     //    return null;
     //}
     //DownFileResponse response = new DownFileResponse();
     //response.FileData = null;
     UserFileOPLog uOpLog = new UserFileOPLog();
     UserIDOP uOp = new UserIDOP();
     uOp.UserID = userID;
     uOp.OPName = "DownLoadFile";
     uOpLog.userOP = uOp;
     uOpLog.FileName = fileName;
     string fileFullPath = Path.Combine(SavePath, fileName);//服务器文件路径
     if (!string.IsNullOrEmpty(savePath))
     {
         fileFullPath = Path.Combine(SavePath, savePath, fileName);
     }
     uOpLog.FilePath = fileFullPath;
     if (!File.Exists(fileFullPath))//判断文件是否存在
     {
         return null;
     }
     FileStream myStream = null;
     try
     {
         uOpLog.sTimeTicks = DateTime.UtcNow.Ticks;
         myStream = File.OpenRead(fileFullPath);
        
     }
     catch (Exception ex) {
         uOpLog.eTimeTicks = DateTime.UtcNow.Ticks;
         uOpLog.exeResult = false;               
         return null;
         throw ex;
     }
     finally
     {
         uOpLog.eTimeTicks = DateTime.UtcNow.Ticks;
         uOpLog.exeResult = true;
         writeUserFileOpLog(uOpLog);
     }
     return myStream;
 }
Esempio n. 6
0
 public void Add(UserFileOPLog item)
 {
     UserOPFileLogQueue.Enqueue(item);
     Interlocked.Increment(ref queueLength);
     lock (SynObject)
     {
         if (queueLength > 10 && !runFlag)
         {
             ThreadPool.QueueUserWorkItem(new WaitCallback(NewThreadFunc));
         }
     }
 }