Ejemplo n.º 1
0
    protected void btnDelete_Click(object sender, EventArgs e)
    {
        FileVO fileVO = m_PostFileService.GetFileById(m_Mode);

        fileVO.Flag        = 0;
        fileVO.NeedUpdate  = true;
        fileVO.UpdatedBy   = m_SessionHelper.LoginUser.FullNameInChinese;
        fileVO.UpdatedDate = DateTime.Now;
        m_PostFileService.UpdateFile(fileVO);
        m_WebLogService.AddSystemLog(MsgVO.Action.刪除, fileVO, "", string.Format("單號:{0}", fileVO.FileId));
        new Thread(new ThreadStart(() => ApiUtil.UpdateFileToServer(Server.MapPath("../../upload/")))).Start();
        ClearUI();
        fillGridView();
    }
        // POST api/<controller>
        public HttpResponseMessage Post(FileDto fileDto)
        {
            if (fileDto != null)
            {
                try
                {
                    FileVO fileVO = null;
                    //檢查是否有ServerId 有的話把狀態改成刪除, 重新建立一筆
                    if (fileDto.ServerId != 0)
                    {
                        FileVO oldFileVO = m_PostFileService.GetFileById(fileDto.ServerId);
                        if (oldFileVO != null)
                        {
                            oldFileVO.NeedUpdate = false;
                            oldFileVO.Flag       = 0;
                            oldFileVO.UpdateId   = "系統API";
                            m_PostFileService.UpdateFile(oldFileVO);
                        }
                    }

                    fileVO            = new FileVO(fileDto);
                    fileVO.FileId     = 0;
                    fileVO.ServerId   = 0;
                    fileVO.NeedUpdate = false;
                    fileVO.UpdateId   = "系統API";
                    FixTimeZone(fileVO);
                    fileVO          = m_PostFileService.CreateFile(fileVO);
                    fileVO.ServerId = fileVO.FileId;

                    return(Request.CreateResponse <FileDto>(HttpStatusCode.Created, new FileDto(fileVO)));
                }
                catch (Exception ex)
                {
                    return(Request.CreateResponse(HttpStatusCode.NoContent, ex.ToString()));
                }
            }
            else
            {
                return(Request.CreateResponse(HttpStatusCode.NoContent));
            }
        }
Ejemplo n.º 3
0
    /// <summary>
    /// 同步到Server
    /// </summary>
    public static void UpdateFileToServer(string filePath)
    {
        PostFactory      m_PostFactory     = new PostFactory();
        IPostFileService m_PostFileService = m_PostFactory.GetPostFileService();
        ConfigHelper     m_ConfigHelper    = new ConfigHelper();
        WebUtility       m_WebUtility      = new WebUtility();

        if (string.IsNullOrEmpty(m_ConfigHelper.PostFileApiUrl))
        {
            return;
        }

        Dictionary <string, string> conditions = new Dictionary <string, string>();

        conditions.Add("NeedUpdate", "true");
        IList <FileVO> list = m_PostFileService.GetFileList(conditions);

        if (list != null && list.Count > 0)
        {
            foreach (FileVO vo in list)
            {
                try
                {
                    FileVO fileVO = m_PostFileService.GetFileById(vo.FileId);
                    if (fileVO.IsUpdatingToServer)
                    {
                        continue;
                    }

                    FileDto dto = new FileDto(vo);

                    //狀態為刪除
                    if (dto.Flag == 0)
                    {
                        vo.IsUpdatingToServer = true;
                        m_PostFileService.UpdateFile(vo);

                        if (dto.ServerId > 0)
                        {
                            //有serverId就去server刪除
                            string     url     = m_ConfigHelper.PostFileApiUrl + "/" + dto.ServerId.ToString();
                            WebRequest request = ApiUtil.Post(url, "DELETE", "");

                            using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
                            {
                                if (response.StatusCode == HttpStatusCode.OK || response.StatusCode == HttpStatusCode.Gone || response.StatusCode == HttpStatusCode.NoContent)
                                {
                                    vo.NeedUpdate         = false;
                                    vo.IsUpdatingToServer = false;
                                    m_PostFileService.UpdateFile(vo);
                                }
                            }
                        }
                        else
                        {
                            //沒有serverId就直接標記已更新
                            vo.NeedUpdate         = false;
                            vo.IsUpdatingToServer = false;
                            m_PostFileService.UpdateFile(vo);
                        }
                    }
                    else
                    {
                        vo.IsUpdatingToServer = true;
                        m_PostFileService.UpdateFile(vo);

                        WebRequest request = ApiUtil.Post <FileDto>(m_ConfigHelper.PostFileApiUrl, "POST", dto);

                        string responseInfo = string.Empty;
                        using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
                        {
                            if (response.StatusCode == HttpStatusCode.Created)
                            {
                                using (Stream stream = response.GetResponseStream())
                                {
                                    responseInfo = (new StreamReader(stream)).ReadToEnd().Trim();

                                    FileDto newFileDto = JsonConvert.DeserializeObject <FileDto>(responseInfo);

                                    vo.IsUpdatingToServer = false;
                                    vo.NeedUpdate         = false;
                                    vo.ServerId           = newFileDto.FileId;
                                    m_PostFileService.UpdateFile(vo);

                                    //成功的話在ftp檔案
                                    m_WebUtility.UploadFileToFTP(Path.Combine(filePath, vo.FileName));
                                }
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    vo.IsUpdatingToServer = false;
                    m_PostFileService.UpdateFile(vo);
                    string error = ex.ToString();
                }
            }
        }
    }