响应重定向与图像插入冲突

时间:2019-01-17 09:19:28

标签: c# asp.net file-upload

我是C#的初学者,我在图像上传方面遇到问题。 我代码中插入到数据库中的所有内容(除了我上传的文件),我不知道为什么它不将其保存在数据库中,仅当我删除Response.Redirect("Personalinformation.aspx")时才保存,但无法删除此响应重定向,因为我需要它。

受保护的无效Button8_Click(对象发送者,EventArgs e)     {

    SqlConnection con = new SqlConnection(conString);
    con.Open();
    if (con.State == System.Data.ConnectionState.Open)
    {
        if (TextBox11.Text != "" && TextBox8.Text != "" && TextBox9.Text != "" )
        {
            if (FileUpload1.PostedFile != null)
            {
                if (FileUpload2.PostedFile != null)
                {
                    string FileName = System.IO.Path.GetFileName(FileUpload1.PostedFile.FileName);
                    string FileName1 = System.IO.Path.GetFileName(FileUpload1.PostedFile.FileName);
                    //Save files to disk
                    FileUpload1.SaveAs(Server.MapPath("/upload/" + FileName));
                    FileUpload2.SaveAs(Server.MapPath("/identification/" + FileName1));

                    String q = "Insert into dbo.requests(request_type,visit_date,reason,request_date,status_id,user_id,user_name,FileName,FilePath,End_vdate,FileName1,FilePath1,RequestNotes,VisitorType)values(N'" + DropDownList1.SelectedValue + "',N'" + TextBox8.Text + "',N'" + TextBox9.Text + "','" + DateTime.Now + "','4','" + Session["empNo"] + "',N'" + Session["ArbnName"] + "' , @FileName , @FilePath ,N'" + TextBox10.Text + "',@FileName1 , @FilePath1)";
                    SqlCommand cmd = new SqlCommand(q, con);
                    cmd.Parameters.AddWithValue("@FileName", FileName);
                    cmd.Parameters.AddWithValue("@FilePath", "/upload/" + FileName);
                    cmd.Parameters.AddWithValue("@FileName1", FileName1);
                    cmd.Parameters.AddWithValue("@FilePath1", "/identification/" + FileName1);
                    cmd.CommandType = CommandType.Text;
                    cmd.ExecuteNonQuery();

                }
            }

            else
            {
                String q = "Insert into dbo.requests(request_type,visit_date,reason,request_date,status_id,user_id,user_name,End_vdate,phone_ext)values(N'" + DropDownList1.SelectedValue + "',N'" + TextBox8.Text + "',N'" + TextBox9.Text + "','" + DateTime.Now + "','4','" + Session["empNo"] + "' ,N'" + Session["ArbnName"] + "',N'" + TextBox10.Text + "','" + TextBox11.Text.ToString() + "')";
                SqlCommand cmd = new SqlCommand(q, con);
                cmd.ExecuteNonQuery();
            }
            response.redirect("Personalinformation.aspx");

        }
        else
        {

            Page.ClientScript.RegisterStartupScript(this.GetType(), "Scripts", "<script>alert(' هناك حقول مطلوبة فارغة  ');</script>");

        }
            String c = "Insert into dbo.logFile(user_id,transaction_date,action_type) values ('" + Session["empNo"] + "', '" + DateTime.Now + "' ,N' طلب قبول زائر') ";
            SqlCommand cmdc = new SqlCommand(c, con);
            cmdc.ExecuteNonQuery();
            con.Close();


        }
}
   protected void Button2_Click(object sender, EventArgs e)
{
    Response.Redirect("Default.aspx");
}

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
    if (DropDownList1.SelectedIndex == 0) //index of long visit
    {
        FileUpload1.Visible = false;
        FileUpload2.Visible = false;

        Button6.Visible = false;
        TextBox10.Visible = false;
        Label2.Visible = false;
        Label3.Visible = false;

    }

    else
    {
        FileUpload1.Visible = true;
        FileUpload2.Visible = true;
        Button6.Visible = true;
        TextBox10.Visible = true;
        Label2.Visible = true;
        Label3.Visible = true;


    }
}

2 个答案:

答案 0 :(得分:0)

您已使用FileUpload1.SaveAs:

       FileUpload1.SaveAs(Server.MapPath("/upload/" + FileName));
       FileUpload2.SaveAs(Server.MapPath("/identification/" + FileName1));

使用以下方法代替它:

       FileUpload1.PostedFile.SaveAs(Server.MapPath("/upload/" + FileName));
       FileUpload2.PostedFile.SaveAs(Server.MapPath("/identification/" + FileName1));

您缺少其中的PostFile关键字。

答案 1 :(得分:0)

如果要将用户转移到其他页面,可以尝试使用Server.Transfer代替Response.Redirect。

difference between Server.Transfer and Response.Redirect?

查看更多详细信息