繁体   English   中英

FormView 中的文件上传

[英]FileUpload in FormView

I have a problem regarding with add an image URL within a database.I'm using fileupload method within formview in ASP.Net.And I have a table called duyurular which can be record a image URL.BTW,I'm using 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)
    {
    }
  }     

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

您必须使用 Formview ItemInserting Event ,您可以在其中传入内置的 URL。

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM