如何上传和插入多个文件到我的数据库? [英] How do I upload and insert several files into my DB?

查看:67
本文介绍了如何上传和插入多个文件到我的数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿伙计们,我正在使用以下代码将文件插入我的数据库:



Hey guys, I'm using the following code to insert files into my DB:

<span>
                <asp:FileUpload ID="FileUpload" runat="server" />
                 <br />
                <asp:Button ID="btnUpload" runat="server" Text="Upload"

                    OnClick="btnUpload_Click" />
                <asp:Label ID="lblMessage" runat="server" Text=""

                    Font-Names="Arial"></asp:Label>
            </span>







,后台代码:






with the background code:

protected void btnUpload_Click(object sender, EventArgs e)
        {
            // Read the file and convert it to Byte Array
            string filePath = FileUpload.PostedFile.FileName;
            string filename = Path.GetFileName(filePath);
            string ext = Path.GetExtension(filename);
            string contenttype = String.Empty;

            //Set the contenttype based on File Extension
            switch (ext)
            {
                case ".doc":
                    contenttype = "application/vnd.ms-word";
                    break;
                case ".docx":
                    contenttype = "application/vnd.ms-word";
                    break;
                case ".xls":
                    contenttype = "application/vnd.ms-excel";
                    break;
                case ".xlsx":
                    contenttype = "application/vnd.ms-excel";
                    break;
                case ".jpg":
                    contenttype = "image/jpg";
                    break;
                case ".png":
                    contenttype = "image/png";
                    break;
                case ".gif":
                    contenttype = "image/gif";
                    break;
                case ".pdf":
                    contenttype = "application/pdf";
                    break;
            }
            if (contenttype != String.Empty)
            {

                Stream fs = FileUpload.PostedFile.InputStream;
                BinaryReader br = new BinaryReader(fs);
                Byte[] bytes = br.ReadBytes((Int32)fs.Length);

                //insert the file into database
                string strQuery = "INSERT INTO Files(Name, Content_Type, Data)" +
                   " VALUES (@Name, @ContentType, @Data)";
                SqlCommand cmd = new SqlCommand(strQuery);
                cmd.Parameters.Add("@Name", SqlDbType.VarChar).Value = filename;
                cmd.Parameters.Add("@ContentType", SqlDbType.VarChar).Value
                  = contenttype;
                cmd.Parameters.Add("@Data", SqlDbType.Binary).Value = bytes;
                InsertUpdateData(cmd);
                lblMessage.ForeColor = System.Drawing.Color.Green;
                lblMessage.Text = "File Uploaded Successfully";
            }
            else
            {
                lblMessage.ForeColor = System.Drawing.Color.Red;
                lblMessage.Text = "File format not recognised." +
                  " Upload Image/Word/PDF/Excel formats";
            }
        }

        private Boolean InsertUpdateData(SqlCommand cmd)
        {
            String strConnString = System.Configuration.ConfigurationManager
            .ConnectionStrings["FORgestIT"].ConnectionString;
            SqlConnection con = new SqlConnection(strConnString);
            cmd.CommandType = CommandType.Text;
            cmd.Connection = con;
            try
            {
                con.Open();
                cmd.ExecuteNonQuery();
                return true;
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
                return false;
            }
            finally
            {
                con.Close();
                con.Dispose();
            }
        }





这里发生的是每次点击上传按钮,文件上传和保存正确地进入数据库,这是完美的工作...问题是这不是我想要的工作,因为这是来自WebForms页面,还有更多的表单字段,我通过不同的方式插入到数据库中按钮。我想要的是让用户能够选择多个文件,但只有当所有其他文件都被上传时才会保存到数据库中...

我正在使用2个表一个用于所有其他文件字段和文件1 ...



请按照我想要的方式给出建议:



What happens here is that everytime i click on the upload button the file uploads and saves correctly into the DB, this is working perfectly... the problem is this isn't how I want it to work since this is from a WebForms page and there are many more form fields which I'm inserting to a DB through a different button. What I want is for users to be able to select several files but they only upload and are saved into the DB when all the others are...
I'm using 2 tables one for all the others fields and 1 for the files...

Please give me suggestions on how to do this the way I want it

推荐答案

这是一个你可以使用的成品:

jQuery multiupload widget [ ^ ]
Here is one finished product that you can use:
jQuery multiupload widget[^]


这篇关于如何上传和插入多个文件到我的数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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