FileUpload及其更新状态

时间:2015-03-25 06:42:58

标签: c# asp.net file-upload

我想创建一个用户上传文件的功能,其名称和描述将显示在gridview中。

现在我想要的是,如果同一个文件有一些更改,则需要再次上传,并且自第二次上传以来。我将再增加一列FileRevision,这将显示文件已被更新的次数。

参见图片供您参考: -

Image for ref

请告诉我从哪里开始。

1 个答案:

答案 0 :(得分:0)

我自己完成了,如下所示: -

ASPX页面

中的

FileUpload Control

<asp:FileUpload ID="fupreportfile" runat="server" CssClass="form-control" ValidationGroup="AddNew" />

现在,在按钮上单击它将检查文件是否存在。检查thorugh CS代码: -

protected void btnSubmit_Click(object sender, EventArgs e)
    {
            SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["DefaultCSRConnection"].ConnectionString);
            using (SqlCommand cmd = conn.CreateCommand())
            {
                if (fupreportfile.HasFiles)
                {
                    int count = CheckFileExists(fupreportfile.PostedFile.FileName);
                    fupreportfile.SaveAs(Server.MapPath("~/ReportFolder/" + fupreportfile.PostedFile.FileName));

                    if (count > 0)
                    {
                        cmd.CommandText = " Update tbl_reports SET revision=@revision Where Id=@Id";
                        cmd.Parameters.AddWithValue("@Id", GetIdByFileName(fupreportfile.PostedFile.FileName));
                        cmd.Parameters.Add("@revision", SqlDbType.VarChar).Value = (count + 1).ToString();
                        cmd.Connection = conn;
                        conn.Open();
                        cmd.ExecuteNonQuery();
                        conn.Close();
                        ScriptManager.RegisterStartupScript(this, this.GetType(), "alert", "alert('Reports updated sucessfully');window.location ='csrreports.aspx';", true);
                    }
                    else
                    {
                        conn.Open();
                        SqlCommand cmd1 = new SqlCommand("Insert into tbl_reports (NgoId,report_type_id,report_title,report_file,report_desc,revision) values(@NgoId, @report_type_id, @report_title,@report_file,@report_desc,@revision)", conn);
                        cmd1.Parameters.Add("@NgoId", SqlDbType.Int).Value = ddlNgoName.SelectedValue;
                        cmd1.Parameters.Add("@report_type_id", SqlDbType.Int).Value = ddlReportType.SelectedValue;
                        cmd1.Parameters.Add("@report_title", SqlDbType.NVarChar).Value = txtreporttitle.Text;
                        cmd1.Parameters.Add("@report_file", SqlDbType.VarChar).Value = fupreportfile.PostedFile.FileName;
                        cmd1.Parameters.Add("@report_desc", SqlDbType.NVarChar).Value = txtreportdescription.Text;
                        cmd1.Parameters.Add("@revision", SqlDbType.VarChar).Value = (count + 1).ToString();
                        cmd1.ExecuteNonQuery();
                        conn.Close();
                        ScriptManager.RegisterStartupScript(this, this.GetType(), "alert", "alert('Reports added sucessfully');window.location ='csrreports.aspx';", true);
                    }
                }
            }
    }

检查文件的代码: -

public int CheckFileExists(string fileName)
    {
        using (SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["DefaultCSRConnection"].ConnectionString))
        {
            SqlCommand cmd = new SqlCommand("SELECT COUNT(*) FROM tbl_reports WHERE report_file=@report_file", con);
            cmd.Parameters.Add("@report_file", SqlDbType.VarChar).Value = fileName;
            con.Open();
            int count = (int)cmd.ExecuteScalar();
            return count;
        }
    }

另外,我需要检查它正在更新哪一行的ID。所以代码是

public int GetIdByFileName(string fileName)
    {
        using (SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["DefaultCSRConnection"].ConnectionString))
        {
            SqlCommand cmd = new SqlCommand("SELECT Id FROM tbl_reports WHERE report_file=@report_file", con);
            cmd.Parameters.Add("@report_file", SqlDbType.VarChar).Value = fileName;
            con.Open();
            int count = (int)cmd.ExecuteScalar();
            return count;
        }
    }

我检查了这个,它对我有用:)