FormView中的FileUpload

时间:2011-07-16 11:34:10

标签: c# .net asp.net file-upload formview

我有一个关于在数据库中添加图像URL的问题。我在ASP.Net中的formview中使用fileupload方法。我有一个名为duyururular的表 这可以记录图像URL.BTW,我正在使用SQL Server数据库。 我的问题是;我正在进行流程更新,删除并在FormView中发布公告。我可以使用FileUpload将这些图像上传到名为“img”的文件夹中。 但是,我想在数据库中记录它。当在数据库中添加另外的那些信息时,没有图像URL。 最后,我无法在数据库中添加图像URL。

这是我的代码;

public partial class panel_yoneticipaneli : System.Web.UI.Page
{

FileUpload dosya, dosya1;
//TextBox t1, t2, t3;
//Button btn;
SqlConnection con;
static string str = "Data Source=SERT;Initial Catalog=Mmakina;Integrated Security=True";
string yol = "";
protected void Page_Load(object sender, EventArgs e)
{
    dosya = (FileUpload)FormView2.FindControl("FileUpload1");
    dosya1 = (FileUpload)FormView2.FindControl("FileUpload2");
   // btn = (Button)FormView2.FindControl("ResimKaydetButonu");
    //t1 = (TextBox)FormView2.FindControl("DuyuruBaslikTextBox");
    //t2 = (TextBox)FormView2.FindControl("DuyuruIcerikTextBox");
    //t3 = (TextBox)FormView2.FindControl("DuyuruTarihiTextBox");

    Label1.Visible = false;
    if (Session["KullaniciID"]!=null)
    {
        con = new SqlConnection(str);
        SqlCommand sorgu = new SqlCommand("SELECT * FROM Kullanici WHERE KullaniciAdi=@KullaniciAdi", con);
        sorgu.Parameters.Add("@KullaniciAdi", SqlDbType.VarChar).Value = Session["KullaniciAdi"];
          con.Open();
        SqlDataReader oku = sorgu.ExecuteReader(CommandBehavior.CloseConnection);
        Label1.Visible = true;
        while (oku.Read())
        {
           Label1.Text = oku["KullaniciAdi"].ToString();

        }

    }
    else {
        Response.Redirect("error.aspx");
    }


}
protected void Button1_Click(object sender, EventArgs e)
{
    FormsAuthentication.SignOut();
    Roles.DeleteCookie();
    Session.Clear();
    Response.Redirect("giris.aspx");

}

protected void btn_Click(object sender,EventArgs e) {
    try
    {
        if (dosya.HasFile)
        {
           dosya.SaveAs(Server.MapPath("~/img/") + dosya.FileName);
           System.Drawing.Image resim = System.Drawing.Image.FromFile(Server.MapPath("~/img/") + dosya.FileName);
           yol = "img/" + dosya.FileName;
           resim.Dispose();
           DbUpload();


        }
    }
    catch (Exception ex)
    {

    }
}



public void DbUpload() {
    try {
                    SqlConnection sc = new SqlConnection("Data Source=SERT;Initial Catalog=Mmakina;Integrated Security=True");
        SqlCommand scom = new SqlCommand("insert into Duyuru(DuyuruResmi) values(@DuyuruResmi)", sc);
        scom.Parameters.AddWithValue("@DuyuruResmi", dosya.FileName);

        scom.ExecuteNonQuery();
        sc.Close();
    }catch(Exception p){
        p.Message.ToString();
    }
}


 protected void UpdateButton_Click(object sender, EventArgs e)
{
try
    {

        if (dosya.HasFile)
        {
           dosya.SaveAs(Server.MapPath("~/img/") + dosya.FileName);
           yol = "img/" + dosya.FileName;
           Response.Write("Fileupload çalışıyor...");
         DbUpload();
        }
    }
    catch (Exception ex)
    {
    }
  }     

提前感谢您可以分享的所有评论。

2 个答案:

答案 0 :(得分:0)

您必须使用Formview ItemInserting Event,您可以在其中传入构建的网址。

protected void frmAsset_ItemInserting(object sender, FormViewInsertEventArgs e)
{
   if (dosya.HasFile)
    {
       dosya.SaveAs(Server.MapPath("~/img/") + dosya.FileName);
       e.NewValues["URL"] = "img/" + dosya.FileName;
    }
 }

答案 1 :(得分:0)

我建议您只是上传图片名称而不指定完整的网址,然后您可以将图片基本路径保存在web.config中,例如'<add key="ImagesBasePath" value="/img" />',这样您就可以更改路径了。可以通过将图像名称连接到ConfigurationManager.AppSettings["ImagesBasePath"]来控制此图像的视图,这样会更好。