http://www.aspsnippets.com/Articles/How-to-save-insert-Image-in-Database-in-ASPNet-using-C-and-VBNet.aspx

1.创建数据库--各字段如下图所示

在数据库中 保存和获取各种文件

 

注:Name:文件名

ContentType:文件的类型 (.doc  .txt)

Data:以二进制的存储的数据

2.添加文件上传控件和上传按钮

<asp:FileUpload ID="FileUpload1" runat="server" />
    <asp:Button ID="btnUpload" runat="server" Text="Upload" OnClick="btnUpload_Click" /><br />
    <asp:Label ID="lblMessage" runat="server" Text="" Font-Names = "Arial"></asp:Label>

3.添加上传文件事件

protected void btnUpload_Click(object sender, EventArgs e)
        {
            //获取上传的文件
            string filePath = FileUpload1.PostedFile.FileName;
            //获取上传文件名
            string filename = Path.GetFileName(filePath);
            //获取文件按的后缀名
            string ext = Path.GetExtension(filename);  
            string contenttype = String.Empty;

            //根据后缀名设置文件类型
            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 = FileUpload1.PostedFile.InputStream;
                BinaryReader br = new BinaryReader(fs);
                Byte[] bytes = br.ReadBytes((Int32)fs.Length);
                //insert the file into database

                string strQuery = "insert into FileSaves(Name, ContentType, 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";
            }
        }
View Code

相关文章:

  • 2022-12-23
  • 2022-02-09
  • 2022-12-23
  • 2021-08-16
  • 2022-01-11
  • 2022-12-23
猜你喜欢
  • 2021-09-10
  • 2021-12-27
  • 2022-01-07
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-08-16
相关资源
相似解决方案