Upload and display multiple files in GridView using AjaxFileUpload in ASP.Net

Last Reply 7 months ago By pandeyism

Posted 7 months ago

i want to upload mulitple files at the time of uploading it should check size and file extension then only it should upload.

once the file is uploaded it should visible in the gridview at the time of gridview it should check the file name with file which are already available in the database, if it matches it should get highlighted with the green color and if it does not match it should highlight with red color there should be option in gridview of  preview and delete option so that user can preview and if it doesnot match it can delete. once the file is matched it should save in the databse

The code which i have written in that folder is created and files are saved in that i dont want to save the files in the folder.

Because the files are save in the folder i can view only one file at a time by giving physical path to it

        protected void AjaxFileUpload1_UploadComplete(object sender, AjaxControlToolkit.AjaxFileUploadEventArgs e)
        {
            string contentTypes = e.ContentType.ToLower();
            string fileName = Path.GetFileName(e.FileName);
            AjaxFileUpload1.SaveAs(Server.MapPath("~/Uploads/" + fileName));
            string filePath = Server.MapPath("~/Uploads/" + fileName);
 
            FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);
            BinaryReader br = new BinaryReader(fs);
            Byte[] bytes = br.ReadBytes((Int32)fs.Length);
 
            String strConnString = "Data Source=*.*.*;Initial Catalog=TEST;User ID=sa;Password=*******";
            SqlConnection con = new SqlConnection(strConnString);
            SqlCommand cmd = new SqlCommand("INSERT INTO FileUploadTest(FileName, FileType, FileDoc) values (@FileName, @FileType, @FileDoc)");
            cmd.Parameters.AddWithValue("@FileName", fileName);
            string contentType = "";
            switch (contentTypes)
            {
                case ".png":
                    contentType = "png";
                    break;
                case ".jpg":
                    contentType = "application/jpg";
                    break;
                case ".jpeg":
                    contentType = "application/jpeg";
                    break;
                case ".bmp":
                    contentType = "Application/bmp";
                    break;
                case ".tiff":
                    contentType = "Application/tiff";
                    break;
                case ".gif":
                    contentType = "Application/gif";
                    break;
                case ".pdf":
                    contentType = "Application/pdf";
                    break;
                case ".doc":
                    contentType = "Application/doc";
                    break;
                case ".docx":
                    contentType = "Application/docx";
                    break;
                default:
                    break;
            }
            cmd.Parameters.AddWithValue("@FileType", contentType);
            cmd.Parameters.AddWithValue("@FileDoc", bytes);
            cmd.CommandType = CommandType.Text;
            cmd.Connection = con;
 
            try
            {
                con.Open();
                cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }
            finally
            {
                con.Close();
                con.Dispose();
            } 
        }
        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            var text = DropDownList1.SelectedItem.Value;
 
            lblMessage.Text = text;
 
            for (int i = 0; i < this.GridView1.Rows.Count; i++)
            {
                this.GridView1.Rows[i].Visible = true;
 
                string cellvalue = GridView1.Rows[i].Cells[1].Text;
 
                if (cellvalue.TrimEnd() != text.TrimEnd())
                {
                     
                    //GridView1.Rows[row.Index].DefaultCellStyle.BackColor = Color.Yellow;
                    
                    GridView1.Rows[i].BackColor = System.Drawing.Color.Red;
                    Console.Write("Values not matched");
                }
                else
                {
                    string constr = "Data Source=****;Initial Catalog=TEST;User ID=sa;Password=*******";
                    using (SqlConnection con = new SqlConnection(constr))
                    {
                        using (SqlCommand cmd = new SqlCommand())
                        {
                            cmd.CommandText = "SELECT * FROM FileUploadTest";
                            cmd.Connection = con;
                            con.Open();
                            GridView1.DataSource = cmd.ExecuteReader();
                            GridView1.DataBind();
                            con.Close();
                        }
                    }
                 //   Console.Write("File Saved Successfully");
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "alert('Record Saved Sucessfully');", true);
                } 
            }
        }
        protected void grdAttachment_RowCommand(object sender,GridViewCommandEventArgs e)
        {
            if (e.CommandName == "ViewFile")
            { string fileName = Server.MapPath(@"~/Uploads/" + e.CommandArgument.ToString());
                Process process = new Process();
                process.StartInfo.UseShellExecute = true;
 
                if (File.Exists(@"Folder\example.pdf"))
                {
                    ProcessStartInfo psStartInfo = new ProcessStartInfo();
                    psStartInfo.FileName = @"Folder\example.pdf";
 
                    Process ps = Process.Start(psStartInfo);
                }
                else
                {
                    //    MessageBox.Show("File not found");
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "alert('File Not found');", true);
                }
            }
        }

 

This question does not have replies that have been liked.