FormView 中的文件上传

发布于 2024-11-24 12:37:37 字数 3086 浏览 1 评论 0原文

我在数据库中添加图像 URL 时遇到问题。我在 ASP.Net 的 formview 中使用 fileupload 方法。而且我有一个名为 duyurular 的表 可以记录图像 URL。顺便说一句,我正在使用 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)
    {
    }
  }     

预先感谢您分享的所有评论。

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 Database.
My question is;I'm doing the process update,delete and to make an announcement in the FormView.I can upload those images within folder called "img" with FileUpload.
However,I want to record it within database as well.when to add within database another those infos,there are no the image URL.
Finally,I can't add the image URL within database.

Here is my code;

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)
    {
    }
  }     

thanks in advance for all comments you can share.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

北凤男飞 2024-12-01 12:37:37

我建议您只上传图像名称而不指定完整的 URL,并且可以将图像基本路径保存在 web.config 中,例如 '' 这样您就可以更改拥有图像的路径,并且可以通过将图像名称连接到 ConfigurationManager.AppSettings["ImagesBasePath"] 来控制该图像的视图,因此这将是更好的。

I suggest that you just upload the image name without specifying the full URL, and you can save the image base path in the web.config like '<add key="ImagesBasePath" value="/img" />' so you can change the path were you have your images and you can control the view of this image by concatenating the Image name to ConfigurationManager.AppSettings["ImagesBasePath"] so this will be better.

鸩远一方 2024-12-01 12:37:37

您必须使用 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;
    }
 }

You have to use Formview ItemInserting Event, where you can pass in the built 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;
    }
 }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文