服务热线:

4006-6500-28

新闻资讯
联系我们

电话:400-665-0028


您当前位置:首页 > 新闻资讯  > 知识学习 > 正文知识学习
K3wise附件打开保存方法VB
添加时间:2023-2-3

附件存储一般为数据库存储,以2进制存储在表t_Accessory 的FData字段中。


sFileName 为本地文件,例如:C:\Users\Desktop\11.txt

sTmpFileName 为保存临时文件路径,用于保存压缩本地文件后的临时文件例如 D:\CODE\temp.tmp

varFile 为 kdzip压缩文件后得到的二进制,用户保存到数据库


VB 保存

Public Function SaveData()

  Dim ozip As Object

  Dim sFileName As String

  Dim sTmpFileName As String

  Dim smFile As Stream

  Dim smFileNoCompress As Stream

  Dim varFile As Variant 

  Dim bytFile() As Byte


  Set ozip = CreateObject("KDZIP.ZIP")

  ozip.Compress sFileName, sTmpFileName, 7

  Set ozip = Nothing

  Set smFile = New ADODB.Stream

  Set smFileNoCompress = New ADODB.Stream

  smFile.Type = adTypeBinary

  smFile.Open

  smFile.LoadFromFile sTmpFileName

  bytFile() = smFile.Read

  Set smFile = Nothing

  varFile = bytFile()

End Function


C#

//SQL存储方式

private DataTable SQLStore()

{

            DataTable rtn = new DataTable();

            string desFilePath = HttpContext.Current.Server.MapPath("./") + FileUpload1.FileName;

            if (FileUpload1.HasFile)

            {

                FileUpload1.MoveTo(desFilePath, MoveToOptions.Overwrite);

                KDZipLib zip = new KDZipLib();

                FileStream FStream = new FileStream(desFilePath, FileMode.Open, FileAccess.Read, FileShare.Read);

                byte[] data = new byte[FStream.Length];

                FStream.Read(data, 0, data.Length);

                FStream.Close();


                string strFileName = Path.GetFileName(FileUpload1.FileName);

                string strFileSize = Math.Floor(FileUpload1.ContentLength / 1024f) + "K";

                DataRow dr = AccessoryData.NewRow();

                DataTable dt = AccessoryData;

                dr["FIndex"] = dt.Rows.Count + 1;

                dr["FID"] = -dt.Rows.Count - 1;

                dr["FUploadTime"] = K3DateTimeFormatInfo.K3LongDateFormat(DateTime.Now);

                //附件数据

                dr["FData"] = zip.Zip(data);

                dr["FFilename"] = strFileName;

                dr["FFileSize"] = Convert.ToInt32(data.Length);

                dr["FDesc"] = this.fileDesc.Value;

                dr["FUploader"] = MMTS.K3Connect.GetProperty("UserID");

                if (this.m_FPage <= 1)

                {

                    dr["FPage"] = 0;

                    dr["FEntryID"] = 0;

                }

                else

                {

                    dr["FPage"] = this.m_FPage - 1;

                    dr["FEntryID"] = this.m_FEntryID;

                }

                dr["FSaveMode"] = 0;//为SQL存储

                dt.Rows.Add(dr);

                rtn = dt;

            }

            return rtn;


 }


VB 打开

Private Function DownLoadFile(ByVal AccessoryID As Long)

    Dim sFileName As String

    Dim obj As Object

    Dim varFile As Variant

    Dim smFile As Stream


    Set obj = CreateObject(ACCESSORYVIEW)

    varFile = obj.GetID(MMTS.PropsString, AccessoryID)  '此处先取出FData中的2进制。

    Set obj = Nothing

    Set smFile = New Stream

    smFile.Type = adTypeBinary

    smFile.Open

    smFile.Write varFile

    smFile.SaveToFile m_sTempFileName, adSaveCreateOverWrite   'm_sTempFileName 为保存文件路径,可自行设置。

    Set smFile = Nothing

End Function


C#打开

其中FVersion 2.0 为采用了KDZipLib压缩过,没压缩请赋值1.0

public void DownLoadFile()

{

KDZipLib zip = new KDZipLib();  

DataRow dr; //dr为t_accessory中取出的附件的记录

  byte[] inputBytes = null;

  string tempStr = DateTime.Now.ToFileTime().ToString() + "_" + dr["FFilename"].ToString();

         sFileName = HttpContext.Current.Server.MapPath("./") + tempStr;

         if (dr["FVersion"].ToString().Equals("2.0"))   

         {

               inputBytes = zip.UnZip((byte[])dr["FData"]);

          }

          else

          {

              inputBytes = (byte[])dr["FData"];

          }

          using (bw = new BinaryWriter(new FileStream(sFileName, FileMode.Create)))

          {

                 bw.Write(inputBytes);

                 bw.Close();

         }

}