View Full Version : بازهمupload
maryam_jamshid
یک شنبه 03 شهریور 1387, 09:33 صبح
من یه فولدر به نام image ساختم که میخوام عکس های مورد نظر م رو از طریق upload به این پوشه بفرستم و وآدرس عکسهام به همراه نام عکس در فیلد address_pic جدولم ذخیره کنم و از طرفی بتونم از طریق یه کدی
عکسهام ببینم.
چطوری میشه اینکارها رو انجام داد؟؟؟
توی سایت گشتم ولی کد ها به دردم نخورد یا بعضی ها error میداد
kezack
یک شنبه 03 شهریور 1387, 10:47 صبح
سلام ببين اين كد بدرد مي خوره
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using Persia;
using System.IO;
using System.Web.Security;
publicpartialclass TirAdmin_AddImage : System.Web.UI.Page
{
int i=0;
protectedvoid Page_Load(object sender, EventArgs e)
{
}
protectedvoid Button1_Click(object sender, EventArgs e)
{
i++;
Persia.SunDate today = new SunDate();
today = Persia.Calendar.ConvertToPersian(DateTime.Now);
SqlConnection con = new SqlConnection(System.Configuration.ConfigurationMa nager.AppSettings["ConnectionString"]);
if (FileUpload1.HasFile)
{
int s = FileUpload1.FileName.LastIndexOf('.');
string type = FileUpload1.FileName.Substring(s);
string name = FileUpload1.FileName.Substring(0, s);
bindname(name, name, type);
if (File.Exists(MapPath("Photo Galleries\\") + name + type))
{
name = name + "(" + i + ")";
bindname(name, name, type);
FileUpload1.SaveAs(Server.MapPath("~/Photo Galleries/") + name + type);
}
else
{
FileUpload1.SaveAs(Server.MapPath("~/Photo Galleries/") + name + type);
}
SqlCommand cmd = new SqlCommand("Insert Into ImageGallary(Scr,Date,Show)Values(@Scr,@Date,@Show );SELECT SCOPE_IDENTITY()");
cmd.Parameters.Add("@Show", SqlDbType.Bit).Value = false;
cmd.Parameters.Add("@Scr", SqlDbType.NVarChar, 50).Value = "~/Photo Galleries/" + name + type;
cmd.Parameters.Add("@Date", SqlDbType.NVarChar).Value = today.Weekday;
cmd.Connection = con;
con.Open();
string j = cmd.ExecuteScalar().ToString();
Image1.ImageUrl="~/Photo Galleries/" + name + type;
con.Close();
}
}
void bindname(string m, string n, string t)
{
if (File.Exists(MapPath("Photo Galleries\\") + m + t))
{
i++;
m = n + "(" + i + ")";
bindname(m, n, t);
}
else
{
FileUpload1.PostedFile.SaveAs(MapPath("~/Photo Galleries/") + m + t);
}
}
}
با اين كد هم 2 عكس يك اسم يكسان نمي كيرند
اين كدلو ببين فكر كنم دستت بياد
يك فيلد به نام scr كه أدرس عكس رو نكه مي داره و بعد كا في براي image از طريق همين فيلد عكس مورد نظر رو نمايش بدي
mr_mtc
یک شنبه 03 شهریور 1387, 15:35 عصر
اينم يك كد كه خودم براي آپلود تصاوير در گالري تصاوير از اون استفاده ميكنم:
protected void btnContactSubmit_Click(object sender, EventArgs e)
{
if(FileUpload1.HasFile)
{
Classes.Data.Picture objImage = new Classes.Data.Picture();
int iImage = objImage.Add(Request.Form["drpPhotoAlbum" +
""].ToString(), Request.Form["txtPhotoTitle" +
""].ToString(), "-", "-", Request.Form["txtPhotoMail" +
""].ToString(), Request.Form["hidPhotoSendLang"].ToString(), "0");
string tmpExt = "";
string ddd = FileUpload1.FileName.ToString();
tmpExt = ddd.Substring(ddd.LastIndexOf(".")).ToLower();
tmpExt = "files\\userpictures\\" + iImage.ToString() + tmpExt.ToString();
FileUpload1.SaveAs(Request.PhysicalApplicationPath + tmpExt.ToString());
string tmpFile = Request.PhysicalApplicationPath + tmpExt.ToString() + "";
System.Drawing.Image tmpImage = System.Drawing.Image.FromFile(tmpFile);
int tmpImageheight = tmpImage.Size.Height;
int tmpImageWidth = tmpImage.Size.Width;
if (tmpImageheight > tmpImageWidth)
{
tmpImageWidth = (int)((double)tmpImageWidth / ((double)tmpImageheight / 100));
tmpImageheight = 100;
}
else
{
tmpImageheight = (int)((double)tmpImageheight / ((double)tmpImageWidth / 100));
tmpImageWidth = 100;
}
System.Drawing.Image tmpThumbnail = tmpImage.GetThumbnailImage(tmpImageWidth, tmpImageheight, new System.Drawing.Image.GetThumbnailImageAbort(Thumbn ailCallback), IntPtr.Zero);
MemoryStream imageStream = new MemoryStream();
tmpThumbnail.Save(imageStream, System.Drawing.Imaging.ImageFormat.Jpeg);
byte[] imgContent = new byte[imageStream.Length];
imageStream.Position = 0;
imageStream.Read(imgContent, 0, (int)imageStream.Length);
string tmpExtSmall = "files\\usersmallpictures\\" + iImage.ToString() + "_small.jpg";
File.WriteAllBytes(Request.PhysicalApplicationPath + tmpExtSmall.ToString(), imgContent);
objImage.Update(iImage.ToString(), Request.Form["drpPhotoAlbum" +
""].ToString(), Request.Form["txtPhotoTitle" +
""].ToString(), tmpExtSmall.ToString(), tmpExt.ToString(), Request.Form["txtPhotoMail" +
""].ToString(), Request.Form["hidPhotoSendLang"].ToString(), "0");
objImage.ConnectionClose();
}
}
public bool ThumbnailCallback()
{
return true;
}
1- براي درج در بانك از كلاس استفاده شده
2- فايل رو به اسم ID ركوردتون در بانك ذخيره ميكنه
3- يك فايل كوچك (عكس ناخني 75*100) هم براي عكستون درست ميكنه و تو يك فولدر ديگه (Small) ذخيره ميكنه
vBulletin® v4.2.5, Copyright ©2000-1404, Jelsoft Enterprises Ltd.