PDA

View Full Version : ذخیره و نمایش عکس upload شده در database



سمیرا55
دوشنبه 28 مرداد 1392, 17:13 عصر
سلام
من یک fileupload در داخل سایتم گذاشتم حالا می خام با افزودن دکمه بار گذاری آدرس عکس در database ذخیره بشه و میخام با نوشتن دستور select لیست تمامی عکس هاس ذخیره شده از database رو نشون بده ولی این کار و که کردم به جای عکس آدرس رو نمایش میده این هم کدمه
که از دو قسمت تشکیل میشه یکی ذخیره مشخصات مخاطب و دیگری upload کردن عکس در
database
اگر لطف کنید کمکم کنید ممنون می شم

( protected void LinkButton2_Click(object sender, EventArgs e)
}
string path = Server.MapPath(".") + "\\upload_images";
string[] validext = { ".jpg", ".gif", ".png", ".rar" };
string ext = System.IO.Path.GetExtension(FileUpload1.PostedFile .FileName);
if (Array.IndexOf(validext, ext.ToLower()) < 0)
{
Response.Redirect("invalied file extension");
return;
}

long size = FileUpload1.PostedFile.ContentLength;
size = size / 1024;
if (size > 240)
{
Response.Redirect("file size must<240KB");
return;
}

string filename = System.IO.Path.GetFileName(FileUpload1.PostedFile. FileName);
while (System.IO.File.Exists(path + "\\" + filename))
filename = "1" + filename;
FileUpload1.PostedFile.SaveAs(path + "\\" + filename);

//string[] files = System.IO.Directory.GetFiles(path);
string tags;

string ff = System.IO.Path.GetFileName(path + "\\" + filename);
tags = "<img width=100px height=100px src='upload_images/{0}'/>\n";
tags = string.Format(tags, ff);

Literal1.Text = tags;
int number = 0;
SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=db1;Integrated Security=True");


SqlDataAdapter da = new SqlDataAdapter();

da.SelectCommand = new SqlCommand();
da.InsertCommand = new SqlCommand();
da.InsertCommand.CommandText = "INSERT INTO phone VALUES(@name,@family,@mobile,@tell,@birthday,@emai l,@website,@address,@owner_id,@picture)";
da.InsertCommand.Parameters.AddWithValue("@name", txtname.Text);
da.InsertCommand.Parameters.AddWithValue("@family", txtfamily.Text);
da.InsertCommand.Parameters.AddWithValue("@mobile", txttell.Text);
da.InsertCommand.Parameters.AddWithValue("@tell", txtmobile.Text);
da.InsertCommand.Parameters.AddWithValue("@email", txtemail.Text);
string f = DropDownList2.Text + "/" + DropDownList3.SelectedValue + "/" + DropDownList6.Text;
da.InsertCommand.Parameters.AddWithValue("@website", txtwebsite.Text);
da.InsertCommand.Parameters.AddWithValue("@address", txtaddress.Text);
da.InsertCommand.Parameters.AddWithValue("@birthday", f);
da.InsertCommand.Parameters.AddWithValue("@picture", path);
if (Session["Userid"] != null)
da.InsertCommand.Parameters.AddWithValue("@owner_id", Session["Userid"]);
else
Console.WriteLine("EROOR");
//da.InsertCommand.Parameters.AddWithValue("@img", FileUpload1.PostedFile);





da.SelectCommand.Connection = con;
da.InsertCommand.Connection = con;
TextBox1.Text = "";
const char dash = '-';
char delimiter = dash;
for (int i = 0; i < CheckBoxList2.Items.Count; i++)
{
if (CheckBoxList2.Items[i].Selected == true)
{
TextBox1.Text += CheckBoxList2.Items[i].Value + "-";
}
}

con.Open();
foreach (string substring in (TextBox1.Text).Split(delimiter))
{

da.SelectCommand.CommandText = "update groups set number=number+1 where groups.groupid='" + substring + "'";

da.SelectCommand.ExecuteNonQuery();
}
con.Close();

// da.InsertCommand.Parameters.AddWithValue("@groupid", TextBox1.Text);

con.Open();
// da.InsertCommand.Connection.Open();
da.InsertCommand.ExecuteNonQuery();
//cmd.ExecuteNonQuery();
// da.Fill(dt);
con.Close();


con.Open();
string commandtext3 = "select id from phone where name='"+txtname.Text+"'";

SqlCommand cmd3 = new SqlCommand(commandtext3, con);

SqlDataReader dr = cmd3.ExecuteReader();
dr.Read();
Session["phoneid"] = dr["id"].ToString();

con.Close();

foreach (ListItem item in CheckBoxList2.Items)
{
if (item.Selected == true)
{
con.Open();
string commandtext = "INSERT INTO details(group_id,phoneid) VALUES(@group_id,@ow_id)";
SqlCommand cmd = new SqlCommand(commandtext, con);
cmd.Parameters.AddWithValue("@ow_id", Session["phoneid"]);

cmd.Parameters.AddWithValue("@group_id", item.Value);

cmd.ExecuteNonQuery();

}
con.Close();

}
}

nice_boy_a
دوشنبه 28 مرداد 1392, 17:35 عصر
به جای عکس آدرس رو نمایش میده این هم کدمه
[/LEFT]

سلام.
این همه کد رو کی میخونه :لبخند:
واسه اینکه آدرس عکس رو نشون نده، کنترل Image بزار، داری از label استفاده میکنی