下载之前获取文件的详细信息 [英] get details of file before downloading it

查看:78
本文介绍了下载之前获取文件的详细信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用c#制作一个下载器,但是我遇到的问题是"获取文件的名称,大小和扩展名以便将它们保存在数据库中"。所以你能帮我解决这个问题吗? ? 

i want to make a down-loader using c# ,but i face a problem which is "getting the name ,size and extension of the file to save them in data base".So can you help me to solve this problem ? 

推荐答案

朋友,

欢迎使用Windows窗体论坛。

Welcome to Windows Forms Forum.

>>获取文件的名称,大小和扩展名以将其保存在数据库中

保存将文件转换为数据库,实际上将文件转换为二进制流,将二进制流保存到数据库中的相应字段,在SQL Server中,该字段的数据类型为 Image ,Access中字段
的数据类型是一个OLE对象。

Save the file to the database, in fact, the file is converted to binary flow, the binary stream will be saved to the corresponding field in the database, in SQL Server, the data type of the field is Image, and the data type of the field in Access is an OLE object.

我做了一个简单的演示,演示将文件保存到SQL Server,请参考如下所示:

I make a simple demo and the demo is saving the file to the SQL Server, please refer to it as below:

在本地数据库中创建一个表:

Create a table in local DB:

然后保存失败到这个数据库:

Then save the fail to this database:

        private void btnSave_Click(object sender, EventArgs e)
        {
            string fileName = @"C:\Users\v-baf\Desktop\Gif\1.gif";
            string strConn = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\v-baf\OneDrive\MyWork\MyWinFrm\WinFormApplication20170302\WinFormApplication20170601+\DAL\Database1.mdf;Integrated Security=True";
            FileInfo fi = new FileInfo(fileName);
            using (FileStream fs = fi.OpenRead())
            {
                byte[] bytes = new byte[fs.Length];
                fs.Read(bytes, 0, Convert.ToInt32(fs.Length));
                using (SqlConnection cn = new SqlConnection(strConn))
                {
                    cn.Open();
                    using (SqlCommand cm = new SqlCommand())
                    {
                        cm.Connection = cn;
                        cm.CommandType = CommandType.Text;
                        cm.CommandText = "insert into T_MyTest (MyFile) values(@file)";
                        SqlParameter spFile = new SqlParameter("@file", SqlDbType.Image);
                        spFile.Value = bytes;
                        cm.Parameters.Add(spFile);
                        cm.ExecuteNonQuery();
                        MessageBox.Show("Saved successfully");
                    }
                }
            }             
        }

然后你会发现表中有一条记录:

Then you can find there is a record in the table:

此记录包含文件的完整信息,包括文件大小,文件内容,文件扩展名和等等。

This record holds the complete information of the file, including the file size, file content, file extension, and etc.

如果您想阅读此记录,请使用以下代码:

And if you want to read this record please use the following code:

        private void btnRead_Click(object sender, EventArgs e)
        {
            string fileName = @"C:\Users\v-baf\Desktop\Gif\2.gif";
            string strConn = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\v-baf\OneDrive\MyWork\MyWinFrm\WinFormApplication20170302\WinFormApplication20170601+\DAL\Database1.mdf;Integrated Security=True";
            using(SqlConnection cn = new SqlConnection(strConn))
            {
                cn.Open();
                using(SqlCommand cm = new SqlCommand())
                {
                    SqlDataReader dr = null;
                    cm.Connection = cn;
                    cm.CommandType = CommandType.Text;
                    cm.CommandText = "select MyFile from T_MyTest where Id=4";
                    dr = cm.ExecuteReader();
                    byte[] File = null;
                    if (dr.Read())
                    {
                        File = (byte[])dr[0];
                    }
                    FileStream fs;
                    FileInfo fi = new FileInfo(fileName);
                    fs = fi.OpenWrite();
                    fs.Write(File, 0, File.Length);
                    fs.Close();
                    MessageBox.Show("Read success");
                }
            }            
        }

您会发现文件夹中出现了相同的文件。

You will find that there is an identical file that appears in your folder.

希望这会有所帮助!

最好的问候,

Stanly


这篇关于下载之前获取文件的详细信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆