ASP.NET网站文件上传下载功能
2014-04-14 11:13
357 查看
if (!IsPostBack)
{
if (Application["RaNum"] == null)
{
Random ra = new Random();
Application["RaNum"] = ra;
}
}
if (pictureUP.HasFile)
{
//Response.Write(FileUpload1.PostedFile.ContentLength);
if (pictureUP.PostedFile.ContentLength < 200 * 1024)
{
//string strPath = Server.MapPath("~/uploadfiles/");
//FileUpload1.SaveAs(strPath+FileUpload1.FileName);
string[] strs = pictureUP.FileName.Split(new char[] { '.' });
string strExt = strs[strs.Length - 1].ToLower();
ArrayList list = new ArrayList();
list.Add("jpg");
list.Add("gif");
list.Add("bmp");
if (list.Contains(strExt))
{
ArrayList listType = new ArrayList();
listType.Add("image/pjpeg");
// TextBox1.Text=pictureUP.PostedFile.ContentType;
listType.Add("image/gif");
listType.Add("image/bmp");
if (listType.Contains(pictureUP.PostedFile.ContentType))
{
Random ra = (Random)Application["RaNum"];
int strRa = ra.Next();
Int64 strDate = Convert.ToInt64(DateTime.Now.ToString("yyyyMMddhhmmss"));
string FileName = strDate.ToString("X") + strRa.ToString("X") + pictureUP.FileName;
string strPath = Server.MapPath("~/images/user/");
string strFilePath = strPath + FileName;
// TextBox1.Text = strFilePath;
while (File.Exists(strFilePath))
{
strRa = ra.Next();
strDate = Convert.ToInt64(DateTime.Now.ToString("yyyyMMddhhmmss"));
FileName = strDate.ToString("X") + strRa.ToString("X") + pictureUP.FileName;
strFilePath = strPath + FileName; }
try
{
pictureUP.SaveAs(strFilePath);
// Response.Write("上传成功!");
userInfoUpData U_Info = new userInfoUpData();
U_Info.UserID = userID.Text;
U_Info.UserPwd = userPWD.Text;
U_Info.UserName = userName.Text;
U_Info.UserSex = userSex.Text;
U_Info.UserAge = Convert.ToInt32(userAge.Text);
U_Info.UserTell = userTell.Text;
U_Info.UserAdr = userAdrr.Text;
U_Info.UserPic = FileName;
UserBLL U_Info_Bll = new UserBLL();
if (U_Info_Bll.UserInfoUp(U_Info) > 0)
{ Response.Write("<script>alert('修改成功')</script>");
}
else
{
Response.Write("<script>alert('修改失败')</script>");
}
}
catch
{
Response.Write("上传失败!");
}
}
}
else
{
Response.Write("<script>alert('上传的文件类型错误!')</script>");
}
}
else
{
Response.Write("<script>alert('上传文件的大小超过了200k!')</script>");
}
}
else
{
Response.Write("<script>alert('请选择一个上传的文件!')</script>");
} 我自己写的源代码你自己慢慢研究吧
{
if (Application["RaNum"] == null)
{
Random ra = new Random();
Application["RaNum"] = ra;
}
}
if (pictureUP.HasFile)
{
//Response.Write(FileUpload1.PostedFile.ContentLength);
if (pictureUP.PostedFile.ContentLength < 200 * 1024)
{
//string strPath = Server.MapPath("~/uploadfiles/");
//FileUpload1.SaveAs(strPath+FileUpload1.FileName);
string[] strs = pictureUP.FileName.Split(new char[] { '.' });
string strExt = strs[strs.Length - 1].ToLower();
ArrayList list = new ArrayList();
list.Add("jpg");
list.Add("gif");
list.Add("bmp");
if (list.Contains(strExt))
{
ArrayList listType = new ArrayList();
listType.Add("image/pjpeg");
// TextBox1.Text=pictureUP.PostedFile.ContentType;
listType.Add("image/gif");
listType.Add("image/bmp");
if (listType.Contains(pictureUP.PostedFile.ContentType))
{
Random ra = (Random)Application["RaNum"];
int strRa = ra.Next();
Int64 strDate = Convert.ToInt64(DateTime.Now.ToString("yyyyMMddhhmmss"));
string FileName = strDate.ToString("X") + strRa.ToString("X") + pictureUP.FileName;
string strPath = Server.MapPath("~/images/user/");
string strFilePath = strPath + FileName;
// TextBox1.Text = strFilePath;
while (File.Exists(strFilePath))
{
strRa = ra.Next();
strDate = Convert.ToInt64(DateTime.Now.ToString("yyyyMMddhhmmss"));
FileName = strDate.ToString("X") + strRa.ToString("X") + pictureUP.FileName;
strFilePath = strPath + FileName; }
try
{
pictureUP.SaveAs(strFilePath);
// Response.Write("上传成功!");
userInfoUpData U_Info = new userInfoUpData();
U_Info.UserID = userID.Text;
U_Info.UserPwd = userPWD.Text;
U_Info.UserName = userName.Text;
U_Info.UserSex = userSex.Text;
U_Info.UserAge = Convert.ToInt32(userAge.Text);
U_Info.UserTell = userTell.Text;
U_Info.UserAdr = userAdrr.Text;
U_Info.UserPic = FileName;
UserBLL U_Info_Bll = new UserBLL();
if (U_Info_Bll.UserInfoUp(U_Info) > 0)
{ Response.Write("<script>alert('修改成功')</script>");
}
else
{
Response.Write("<script>alert('修改失败')</script>");
}
}
catch
{
Response.Write("上传失败!");
}
}
}
else
{
Response.Write("<script>alert('上传的文件类型错误!')</script>");
}
}
else
{
Response.Write("<script>alert('上传文件的大小超过了200k!')</script>");
}
}
else
{
Response.Write("<script>alert('请选择一个上传的文件!')</script>");
} 我自己写的源代码你自己慢慢研究吧
相关文章推荐
- ASP.NET网站 文件的上传与下载(一)知识准备
- asp.net实现网站上传下载功能的几种方法
- Asp.net实现MVC处理文件的上传下载功能实例教程
- ASP.NET网站 文件的上传与下载(二)
- 拥有网页版小U盘 ASP.NET实现文件上传与下载功能
- 添加ASP.NET文件上传功能为您的网站
- 拥有网页版小U盘 ASP.NET实现文件上传与下载功能
- ASP .net 实现文件上传 下载功能
- Asp.net实现MVC处理文件的上传下载功能实例教程
- Asp.net 中文件的上传和下载
- ASP.NET上传下载文件
- Asp.Net文件的上传和下载
- FCKeditor是使用非常广泛的HTML编辑器,本文从 ASP.NET 的使用场景对 FCKeditor 与 FCKeditor.NET 的配置、功能扩展(如自定义文件上传子目录、自定义文件名、上传图片的后期处理等)、以及安全性进行初步的阐述。
- asp.net 多文件上传,兼容IE6/7/8,提供完整代码下载
- ASP.NET上传和下载文件的代码
- ASP.NET中实现文件下载功能
- Asp.net 2.0 用 FileUpload 控件实现多文件上传 用户控件(示例代码下载)
- ASP.NET MVC 实践之路 之十八 利用ASP.NET MVC处理文件上传与下载
- ASP.NET中上传下载文件
- ASP.NET文件上传下载