Пример #1
0
    /// <summary>
    /// 文件保存
    /// </summary>
    /// <param name="fileNameDesc"></param>
    /// <param name="File1"></param>
    /// <returns></returns>
    private SysFileManager FileSave(string fileNameDesc, HtmlInputFile File1, Entity myen)
    {
        SysFileManager en = new SysFileManager();

        en.EnName = myen.ToString();
        // en.FileID = this.RefPK + "_" + count.ToString();
        EnCfg cfg = new EnCfg(en.EnName);

        string filePath = cfg.FJSavePath; // BP.SystemConfig.PathOfFDB + "\\" + this.EnName + "\\";

        if (System.IO.Directory.Exists(filePath) == false)
        {
            System.IO.Directory.CreateDirectory(filePath);
        }

        string ext = System.IO.Path.GetExtension(File1.PostedFile.FileName);

        ext          = ext.Replace(".", "");
        en.MyFileExt = ext;
        if (fileNameDesc == "" || fileNameDesc == null)
        {
            en.MyFileName = System.IO.Path.GetFileNameWithoutExtension(File1.PostedFile.FileName);
        }
        else
        {
            en.MyFileName = fileNameDesc;
        }
        en.RDT        = DataType.CurrentData;
        en.RefVal     = myen.PKVal.ToString();
        en.MyFilePath = filePath;
        en.Insert();

        string fileName = filePath + en.OID + "." + en.MyFileExt;

        File1.PostedFile.SaveAs(fileName);

        File1.PostedFile.InputStream.Close();
        File1.PostedFile.InputStream.Dispose();
        File1.Dispose();

        System.IO.FileInfo fi = new System.IO.FileInfo(fileName);
        en.MyFileSize = DataType.PraseToMB(fi.Length);

        if (DataType.IsImgExt(en.MyFileExt))
        {
            System.Drawing.Image img = System.Drawing.Image.FromFile(fileName);
            en.MyFileH = img.Height;
            en.MyFileW = img.Width;
            img.Dispose();
        }
        en.WebPath = cfg.FJWebPath + en.OID + "." + en.MyFileExt;
        en.Update();
        return(en);
    }
Пример #2
0
    public void Save()
    {
        Entity en = this.GetEns.GetNewEntity;

        if (this.PKVal != null)
        {
            en.PKVal = this.PKVal;
            en.RetrieveFromDBSources();
        }

        en = this.UCEn1.GetEnData(en);

        this.CurrEn = en;
        en.Save();
        this.PKVal = en.PKVal;

        #region 保存 实体附件
        try
        {
            if (en.EnMap.Attrs.Contains("MyFileName"))
            {
                HtmlInputFile file = this.UCEn1.FindControl("file") as HtmlInputFile;
                if (file != null && file.Value.IndexOf(".") != -1)
                {
                    BP.Sys.EnCfg cfg = new EnCfg(en.ToString());
                    if (System.IO.Directory.Exists(cfg.FJSavePath) == false)
                    {
                        System.IO.Directory.CreateDirectory(cfg.FJSavePath);
                    }

                    /* 如果包含这二个字段。*/
                    string fileName = file.PostedFile.FileName;
                    fileName = fileName.Substring(fileName.LastIndexOf("\\") + 1);

                    string filePath = cfg.FJSavePath;
                    en.SetValByKey("MyFilePath", filePath);

                    string ext = "";
                    if (fileName.IndexOf(".") != -1)
                    {
                        ext = fileName.Substring(fileName.LastIndexOf(".") + 1);
                    }

                    en.SetValByKey("MyFileExt", ext);
                    en.SetValByKey("MyFileName", fileName);
                    en.SetValByKey("WebPath", cfg.FJWebPath + en.PKVal + "." + ext);

                    string fullFile = filePath + "/" + en.PKVal + "." + ext;

                    file.PostedFile.SaveAs(fullFile);
                    file.PostedFile.InputStream.Close();
                    file.PostedFile.InputStream.Dispose();
                    file.Dispose();

                    System.IO.FileInfo info = new System.IO.FileInfo(fullFile);
                    en.SetValByKey("MyFileSize", BP.DA.DataType.PraseToMB(info.Length));
                    if (DataType.IsImgExt(ext))
                    {
                        System.Drawing.Image img = System.Drawing.Image.FromFile(fullFile);
                        en.SetValByKey("MyFileH", img.Height);
                        en.SetValByKey("MyFileW", img.Width);
                        img.Dispose();
                    }
                    en.Update();
                }
            }
        }
        catch (Exception ex)
        {
            this.Alert("保存附件出现错误:" + ex.Message);
        }
        #endregion


        #region 保存 属性 附件
        try
        {
            AttrFiles       fils = en.EnMap.HisAttrFiles;
            SysFileManagers sfs  = new SysFileManagers(en.ToString(), en.PKVal.ToString());
            foreach (AttrFile fl in fils)
            {
                HtmlInputFile file = (HtmlInputFile)this.UCEn1.FindControl("F" + fl.FileNo);
                if (file.Value.Contains(".") == false)
                {
                    continue;
                }

                SysFileManager enFile = sfs.GetEntityByKey(SysFileManagerAttr.AttrFileNo, fl.FileNo) as SysFileManager;
                SysFileManager enN    = null;
                if (enFile == null)
                {
                    enN = this.FileSave(null, file, en);
                }
                else
                {
                    enFile.Delete();
                    enN = this.FileSave(null, file, en);
                }

                enN.AttrFileNo   = fl.FileNo;
                enN.AttrFileName = fl.FileName;
                enN.EnName       = en.ToString();
                enN.Update();
            }
        }
        catch (Exception ex)
        {
            this.Alert("保存附件出现错误:" + ex.Message);
        }
        #endregion
    }