后台程序代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Diagnostics;
using System.Text.RegularExpressions;
using System.IO;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web.Security;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls.WebParts;
using System.Text;
using System.Drawing;
using System.Security.Cryptography;
public partial class issue_rentOutHouse : System.Web.UI.Page
{
private int picCount;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
SaveCurrentPageFileControls();
picCount = 0;
}
}
protected void BtnHouseRentOut_Click(object sender, EventArgs e)
{
int newsID;
newsID = submitHouseRentOut(0);//预先按照没有图片上传数据库信息,
UpLoadFiles(newsID);
}
protected int submitHouseRentOut(int pic_Count )
{ //创建一个房屋出租信息实体类
ob_houseRentOut houseData = new ob_houseRentOut();
…………………………
houseRentOutBLL RentOutsystem = new houseRentOutBLL();
return (RentOutsystem.AddHouseRentOut(houseData));//将新发布的房屋出租信息ID返回给调用函数,供图片使用
}
protected bool UploadPic(HttpPostedFile userPostedFile,string filename,int newsID,string ext)
{ ……………………………………
}
protected void BtnAddFileUpload_Click(object sender, EventArgs e)
{
AddOneFileControl();
UpdatePanel_picAdd.Update(); }
private void AddOneFileControl()//////添加一个上传文件控件
{
ArrayList al = new ArrayList();
this.tbFiles.Rows.Clear();
GetFileControlsFromSession();
HtmlTableRow htr = new HtmlTableRow();
HtmlTableCell htc = new HtmlTableCell();
htc.Controls.Add(new FileUpload());
htr.Controls.Add(htc);
this.tbFiles.Rows.Add(htr);
SaveCurrentPageFileControls();
} private void GetFileControlsFromSession() ///////读取缓存中存储的上传文件控件集
{
ArrayList al = new ArrayList();
if (Session["FilesControls"] != null)
{
al = (System.Collections.ArrayList)Session["FilesControls"];
for (int i = 0; i < al.Count; i++)
{
HtmlTableRow htr1 = new HtmlTableRow();
HtmlTableCell htc1 = new HtmlTableCell();
htc1.Controls.Add((System.Web.UI.WebControls.FileUpload)al[i]);
htr1.Controls.Add(htc1);
this.tbFiles.Rows.Add(htr1);
}
}
}
private void SaveCurrentPageFileControls() ///保存当前页面上传文件控件集到缓存中
{
ArrayList al = new ArrayList();
foreach (Control controlTR in this.tbFiles.Controls)
{
if (controlTR.GetType().ToString() == "System.Web.UI.HtmlControls.HtmlTableRow")
{
HtmlTableCell htc = (HtmlTableCell)controlTR.Controls[0];
foreach (Control controlFileUpload in htc.Controls)
{
if (controlFileUpload.GetType().ToString() == "System.Web.UI.WebControls.FileUpload")
{
FileUpload tempFileUpload = (FileUpload)controlFileUpload;
al.Add(tempFileUpload);
}
}
}
}
Session.Add("FilesControls", al);
} private void UpLoadFiles(int newsID) /////上传文件操作
{
string filepath = Server.MapPath("../") + "upload";
HttpFileCollection uploadedFiles = Request.Files;
for (int i = 0; i < uploadedFiles.Count; i++)
{
HttpPostedFile userPostedFile = uploadedFiles[i];
string filename = "";
string fileExt = "";
Boolean fileOK = false;
filename = System.IO.Path.GetFileName(userPostedFile.FileName); //获取文件名
fileExt = System.IO.Path.GetExtension(filename).ToLower(); //获取文件后缀
filename = System.DateTime.Now.ToString("yyyyMMddHHmmssfff");
string webFilePath_linshi = Server.MapPath("../upload/" + filename); // 服务器端文件路径
while (File.Exists(webFilePath_linshi + fileExt))
{
filename += "_1";
}
string[] allowedExtension = { ".gif", ".png", ".jpeg", ".jpg" };
for (int j = 0; j < allowedExtension.Length; j++)
{
if (fileExt == allowedExtension[j])
{
fileOK = true;
}
}
try
{
if (userPostedFile.ContentLength > 0) //检查文件大小及扩展名
{
if (fileOK)
{
// userPostedFile.SaveAs(filepath + "\\" + filename + fileExt);
picCount++; //记录图片数量
//图片信息保存至数据库
// protected bool UploadPic(HttpPostedFile userPostedFile,string filename,int newsID)
// protected bool savePicInfosql(string oldName, string name, string path, string bzpath,string syppath, string sypath, string spath, int houseid)
UploadPic(userPostedFile, filename, newsID,fileExt);
// pic_zero(filename + fileExt); }
else
{
Response.Write("<script language=javascript>alert('上传格式不正确只支持gif,jpg,jpeg,png');</script>");
}
} }
catch
{
Response.Write("<script language=javascript>alert('上传出错');</script>");
}
}
if (Session["FilesControls"] != null)
{
Session.Remove("FilesControls");
}
} /////////////////////////////////////////////微缩图
public void pic_zero(string picname)
{
string filepath = Server.MapPath("../upload/") + picname;
System.Drawing.Image image = System.Drawing.Image.FromFile(filepath);
int iImgWidth = image.Width;
int iImgHeight = image.Height;
int iScale = iImgWidth / 140; //取得图片大小
System.Drawing.Size size = new Size(image.Width / iScale, image.Height / iScale);
//新建一个bmp图片
System.Drawing.Image bitmap = new System.Drawing.Bitmap(size.Width, size.Height);
//新建一个画板
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);
//设置高质量插值法
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
//设置高质量,低速度呈现平滑程度
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; g.Clear(Color.Transparent); //清空画布并以透明背景色填充
//在指定位置画图
g.DrawImage(image, new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height),
new System.Drawing.Rectangle(0, 0, image.Width, image.Height),
System.Drawing.GraphicsUnit.Pixel);
//保存高清晰度的缩略图
bitmap.Save(Server.MapPath("../upload/small/" + picname), System.Drawing.Imaging.ImageFormat.Jpeg);
g.Dispose();
image.Dispose(); }
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Diagnostics;
using System.Text.RegularExpressions;
using System.IO;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web.Security;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls.WebParts;
using System.Text;
using System.Drawing;
using System.Security.Cryptography;
public partial class issue_rentOutHouse : System.Web.UI.Page
{
private int picCount;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
SaveCurrentPageFileControls();
picCount = 0;
}
}
protected void BtnHouseRentOut_Click(object sender, EventArgs e)
{
int newsID;
newsID = submitHouseRentOut(0);//预先按照没有图片上传数据库信息,
UpLoadFiles(newsID);
}
protected int submitHouseRentOut(int pic_Count )
{ //创建一个房屋出租信息实体类
ob_houseRentOut houseData = new ob_houseRentOut();
…………………………
houseRentOutBLL RentOutsystem = new houseRentOutBLL();
return (RentOutsystem.AddHouseRentOut(houseData));//将新发布的房屋出租信息ID返回给调用函数,供图片使用
}
protected bool UploadPic(HttpPostedFile userPostedFile,string filename,int newsID,string ext)
{ ……………………………………
}
protected void BtnAddFileUpload_Click(object sender, EventArgs e)
{
AddOneFileControl();
UpdatePanel_picAdd.Update(); }
private void AddOneFileControl()//////添加一个上传文件控件
{
ArrayList al = new ArrayList();
this.tbFiles.Rows.Clear();
GetFileControlsFromSession();
HtmlTableRow htr = new HtmlTableRow();
HtmlTableCell htc = new HtmlTableCell();
htc.Controls.Add(new FileUpload());
htr.Controls.Add(htc);
this.tbFiles.Rows.Add(htr);
SaveCurrentPageFileControls();
} private void GetFileControlsFromSession() ///////读取缓存中存储的上传文件控件集
{
ArrayList al = new ArrayList();
if (Session["FilesControls"] != null)
{
al = (System.Collections.ArrayList)Session["FilesControls"];
for (int i = 0; i < al.Count; i++)
{
HtmlTableRow htr1 = new HtmlTableRow();
HtmlTableCell htc1 = new HtmlTableCell();
htc1.Controls.Add((System.Web.UI.WebControls.FileUpload)al[i]);
htr1.Controls.Add(htc1);
this.tbFiles.Rows.Add(htr1);
}
}
}
private void SaveCurrentPageFileControls() ///保存当前页面上传文件控件集到缓存中
{
ArrayList al = new ArrayList();
foreach (Control controlTR in this.tbFiles.Controls)
{
if (controlTR.GetType().ToString() == "System.Web.UI.HtmlControls.HtmlTableRow")
{
HtmlTableCell htc = (HtmlTableCell)controlTR.Controls[0];
foreach (Control controlFileUpload in htc.Controls)
{
if (controlFileUpload.GetType().ToString() == "System.Web.UI.WebControls.FileUpload")
{
FileUpload tempFileUpload = (FileUpload)controlFileUpload;
al.Add(tempFileUpload);
}
}
}
}
Session.Add("FilesControls", al);
} private void UpLoadFiles(int newsID) /////上传文件操作
{
string filepath = Server.MapPath("../") + "upload";
HttpFileCollection uploadedFiles = Request.Files;
for (int i = 0; i < uploadedFiles.Count; i++)
{
HttpPostedFile userPostedFile = uploadedFiles[i];
string filename = "";
string fileExt = "";
Boolean fileOK = false;
filename = System.IO.Path.GetFileName(userPostedFile.FileName); //获取文件名
fileExt = System.IO.Path.GetExtension(filename).ToLower(); //获取文件后缀
filename = System.DateTime.Now.ToString("yyyyMMddHHmmssfff");
string webFilePath_linshi = Server.MapPath("../upload/" + filename); // 服务器端文件路径
while (File.Exists(webFilePath_linshi + fileExt))
{
filename += "_1";
}
string[] allowedExtension = { ".gif", ".png", ".jpeg", ".jpg" };
for (int j = 0; j < allowedExtension.Length; j++)
{
if (fileExt == allowedExtension[j])
{
fileOK = true;
}
}
try
{
if (userPostedFile.ContentLength > 0) //检查文件大小及扩展名
{
if (fileOK)
{
// userPostedFile.SaveAs(filepath + "\\" + filename + fileExt);
picCount++; //记录图片数量
//图片信息保存至数据库
// protected bool UploadPic(HttpPostedFile userPostedFile,string filename,int newsID)
// protected bool savePicInfosql(string oldName, string name, string path, string bzpath,string syppath, string sypath, string spath, int houseid)
UploadPic(userPostedFile, filename, newsID,fileExt);
// pic_zero(filename + fileExt); }
else
{
Response.Write("<script language=javascript>alert('上传格式不正确只支持gif,jpg,jpeg,png');</script>");
}
} }
catch
{
Response.Write("<script language=javascript>alert('上传出错');</script>");
}
}
if (Session["FilesControls"] != null)
{
Session.Remove("FilesControls");
}
} /////////////////////////////////////////////微缩图
public void pic_zero(string picname)
{
string filepath = Server.MapPath("../upload/") + picname;
System.Drawing.Image image = System.Drawing.Image.FromFile(filepath);
int iImgWidth = image.Width;
int iImgHeight = image.Height;
int iScale = iImgWidth / 140; //取得图片大小
System.Drawing.Size size = new Size(image.Width / iScale, image.Height / iScale);
//新建一个bmp图片
System.Drawing.Image bitmap = new System.Drawing.Bitmap(size.Width, size.Height);
//新建一个画板
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);
//设置高质量插值法
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
//设置高质量,低速度呈现平滑程度
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; g.Clear(Color.Transparent); //清空画布并以透明背景色填充
//在指定位置画图
g.DrawImage(image, new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height),
new System.Drawing.Rectangle(0, 0, image.Width, image.Height),
System.Drawing.GraphicsUnit.Pixel);
//保存高清晰度的缩略图
bitmap.Save(Server.MapPath("../upload/small/" + picname), System.Drawing.Imaging.ImageFormat.Jpeg);
g.Dispose();
image.Dispose(); }
private void UpLoadFiles(int newsID) /////上传文件操作
{
string filepath = Server.MapPath("../") + "upload";
HttpFileCollection uploadedFiles = Request.Files;
for (int i = 0; i < uploadedFiles.Count; i++)
参数后面再加个随机数试试看(如upload.aspx?r=一个随机数)。