masoudsalahi
چهارشنبه 17 تیر 1394, 17:24 عصر
سلام بر دوستان و اساتید
دوستان این کد ارسال منه در پایگاه داده که همینطور که میبینید من قبل از آپلود حجم تصاویر چک می کنم اگر بیشتر از یه حدی بود ارور بده و ارسال انجام نشه حالا می خوام بجای چک کردن حجم فقط حجم تصاویر کاهش بدم این کار به چه صورتی ممکنه ؟ لطفا کد بدید من تو کدنویسی خیلی ضعیف هستم . ممنون
protected void Button1_Click(object sender, EventArgs e)
{
if (photoupload.HasFile)
{
if (!CheckFileType(photoupload.FileName))
{
lblerror.Text += "<br />" + " ! نوع فایل نادرست می باشد ";
}
else if (!CheckFileSize(photoupload))
{
lblerror.Text += "<br />" + " اندازه فایل بیشتر از 100 کیلوبایت می باشد";
}
else
{
Random rnd = new Random();
string ptitle, pdescr, pemail, pdate;
if (txttitle.Text.Trim().Length >= 70) ptitle = txttitle.Text.Trim().Substring(0, 70);
else ptitle = txttitle.Text.Trim();
if (txttozihat.Text.Trim().Length >= 2000) pdescr = txttozihat.Text.Trim().Substring(0, 2000);
else pdescr = txttozihat.Text.Trim();
byte catid = Convert.ToByte(DDLNewsCat.SelectedValue.ToString() );
string fileName = Path.GetFileNameWithoutExtension(photoupload.FileN ame) + rnd.Next(1000, 10000).ToString();
string ext = Path.GetExtension(photoupload.FileName);
fileName = fileName + ext;
if (txtemail.Text.Trim().Length >= 120) pemail = txtemail.Text.Trim().Substring(0, 120);
else pemail = txtemail.Text.Trim();
pdate = lbldate.Text;
DataSet1TableAdapters.tblProductTableAdapter adapter = new DataSet1TableAdapters.tblProductTableAdapter();
adapter .Addproduct (ptitle, pdescr, fileName, catid, 0,pemail ,pdate );
photoupload.PostedFile.SaveAs(MapPath("~/photos/" + fileName));
btnsubmit.Enabled = false;
Response.Redirect("~/UserProfile/supportSubmit.aspx");
}
}
else if (!photoupload.HasFile || txttitle.Text == "" || txttozihat.Text == "" || txtemail.Text == "" || txtmoney.Text == "") ;
{
lblerror.Text += "<br />" + "لطفا اطلاعات را کامل وارد نمایید";
}
}
bool CheckFileType (string fileName) {
string ext = Path .GetExtension (fileName);
switch (ext .ToLower ()) {
case ".gif": return true ;
case ".png": return true ;
case ".jpg": return true ;
case ".jpeg": return true ;
default : return false ;
}
}
bool CheckFileSize(FileUpload fileName){
if (fileName .PostedFile .ContentLength > 102400) return false ;
else return true ;
}
دوستان این کد ارسال منه در پایگاه داده که همینطور که میبینید من قبل از آپلود حجم تصاویر چک می کنم اگر بیشتر از یه حدی بود ارور بده و ارسال انجام نشه حالا می خوام بجای چک کردن حجم فقط حجم تصاویر کاهش بدم این کار به چه صورتی ممکنه ؟ لطفا کد بدید من تو کدنویسی خیلی ضعیف هستم . ممنون
protected void Button1_Click(object sender, EventArgs e)
{
if (photoupload.HasFile)
{
if (!CheckFileType(photoupload.FileName))
{
lblerror.Text += "<br />" + " ! نوع فایل نادرست می باشد ";
}
else if (!CheckFileSize(photoupload))
{
lblerror.Text += "<br />" + " اندازه فایل بیشتر از 100 کیلوبایت می باشد";
}
else
{
Random rnd = new Random();
string ptitle, pdescr, pemail, pdate;
if (txttitle.Text.Trim().Length >= 70) ptitle = txttitle.Text.Trim().Substring(0, 70);
else ptitle = txttitle.Text.Trim();
if (txttozihat.Text.Trim().Length >= 2000) pdescr = txttozihat.Text.Trim().Substring(0, 2000);
else pdescr = txttozihat.Text.Trim();
byte catid = Convert.ToByte(DDLNewsCat.SelectedValue.ToString() );
string fileName = Path.GetFileNameWithoutExtension(photoupload.FileN ame) + rnd.Next(1000, 10000).ToString();
string ext = Path.GetExtension(photoupload.FileName);
fileName = fileName + ext;
if (txtemail.Text.Trim().Length >= 120) pemail = txtemail.Text.Trim().Substring(0, 120);
else pemail = txtemail.Text.Trim();
pdate = lbldate.Text;
DataSet1TableAdapters.tblProductTableAdapter adapter = new DataSet1TableAdapters.tblProductTableAdapter();
adapter .Addproduct (ptitle, pdescr, fileName, catid, 0,pemail ,pdate );
photoupload.PostedFile.SaveAs(MapPath("~/photos/" + fileName));
btnsubmit.Enabled = false;
Response.Redirect("~/UserProfile/supportSubmit.aspx");
}
}
else if (!photoupload.HasFile || txttitle.Text == "" || txttozihat.Text == "" || txtemail.Text == "" || txtmoney.Text == "") ;
{
lblerror.Text += "<br />" + "لطفا اطلاعات را کامل وارد نمایید";
}
}
bool CheckFileType (string fileName) {
string ext = Path .GetExtension (fileName);
switch (ext .ToLower ()) {
case ".gif": return true ;
case ".png": return true ;
case ".jpg": return true ;
case ".jpeg": return true ;
default : return false ;
}
}
bool CheckFileSize(FileUpload fileName){
if (fileName .PostedFile .ContentLength > 102400) return false ;
else return true ;
}