这是多文件上传的例子 string strfname="";
try
{
const string uploadPath=@"\que\upfile\"; //定义上传的物理路径
const string uploadType=".rar,.doc,.txt,.zip,.gif,.jpg"; //定义容许上传的文件类型
const int maxFileSize=1024*200; //设定最大200K; HttpFileCollection files=System.Web.HttpContext.Current.Request.Files;
int iFile;
string fileExtension; //文件扩展名
string tempfilename;
string fullfilename;
string filenewname;
string fileoldname;
string fullpath; Random r=new Random();
fullpath=HttpContext.Current.Request.PhysicalApplicationPath + uploadPath; for (iFile = 0;iFile<files.Count;iFile ++)
{
HttpPostedFile postedFile = files[iFile];
fileoldname = System.IO.Path.GetFileName(postedFile.FileName);
tempfilename=DateTime.Now.Year.ToString()+DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString();
tempfilename=tempfilename+r.Next(10000); //随机产生新的文件主名
if (fileoldname != "")
{
fileExtension = System.IO.Path.GetExtension(fileoldname).ToLower(); //取得上传文件的扩展名
fullfilename=fullpath +"\\" + tempfilename + fileExtension; //保存文件的新文件全名
filenewname=tempfilename + fileExtension; //产生新文件名 if(uploadType.IndexOf(fileExtension)==-1)
{
Response.Write("<script>alert('对不起,你要上传的文件不在许可的类型范围内!');history.back();</script>");
Response.End();
}
else
{ if(postedFile.ContentLength>maxFileSize)
{
Response.Write("<script>alert('对不起,文件超过限定大小!');history.back();</script>");
Response.End();
}
else
{
//上传文件
postedFile.SaveAs(fullfilename);
strfname+=filenewname+',';
} }
}
}
try
{
const string uploadPath=@"\que\upfile\"; //定义上传的物理路径
const string uploadType=".rar,.doc,.txt,.zip,.gif,.jpg"; //定义容许上传的文件类型
const int maxFileSize=1024*200; //设定最大200K; HttpFileCollection files=System.Web.HttpContext.Current.Request.Files;
int iFile;
string fileExtension; //文件扩展名
string tempfilename;
string fullfilename;
string filenewname;
string fileoldname;
string fullpath; Random r=new Random();
fullpath=HttpContext.Current.Request.PhysicalApplicationPath + uploadPath; for (iFile = 0;iFile<files.Count;iFile ++)
{
HttpPostedFile postedFile = files[iFile];
fileoldname = System.IO.Path.GetFileName(postedFile.FileName);
tempfilename=DateTime.Now.Year.ToString()+DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString();
tempfilename=tempfilename+r.Next(10000); //随机产生新的文件主名
if (fileoldname != "")
{
fileExtension = System.IO.Path.GetExtension(fileoldname).ToLower(); //取得上传文件的扩展名
fullfilename=fullpath +"\\" + tempfilename + fileExtension; //保存文件的新文件全名
filenewname=tempfilename + fileExtension; //产生新文件名 if(uploadType.IndexOf(fileExtension)==-1)
{
Response.Write("<script>alert('对不起,你要上传的文件不在许可的类型范围内!');history.back();</script>");
Response.End();
}
else
{ if(postedFile.ContentLength>maxFileSize)
{
Response.Write("<script>alert('对不起,文件超过限定大小!');history.back();</script>");
Response.End();
}
else
{
//上传文件
postedFile.SaveAs(fullfilename);
strfname+=filenewname+',';
} }
}
}
解决方案 »
- 查询数据库后。如何放入后台代码的变量中
- 再求正则表达式。
- 关于access和datagridview操作问题
- 一个字符串包含<br>我要把他绑到ListBox上显示,当有<br>的时候就要在ListBox上换行
- 同行帮个忙,关于图形处理的,前辈请帮下小弟
- 关于treeview
- 如何用SendKeys或者SendMessage使WebBrowser中的页面向下翻页,WebBrowser中加载的是PowerPoint(PPT)文件
- 是不是我的vs.net坏了(高分赠送!!100分不够再加)
- 简单问题,大家帮忙看看(答对就结贴)
- wpf中多个DatePicker 被绑定后,后台获取的日期不对
- 关于窗体关闭问题,在线等待
- 散分请教!!
{
if(FileUp.PostedFile.ContentLength!=0)
{
if(FileUp.PostedFile.ContentType=="image/pjpeg" || FileUp.PostedFile.ContentType=="image/gif"||FileUp.PostedFile.ContentType=="image/bmp") //设置上传文件类型
{
string filename=FileUp.PostedFile.FileName; //取得文件名
int i=filename.Length;
filename=filename.Remove(0,i-4);
string s=DateTime.Now.Year.ToString()+DateTime.Now.Month.ToString()+DateTime.Now.Day.ToString()+DateTime.Now.Hour.ToString()+DateTime.Now.Minute.ToString()+DateTime.Now.Second.ToString()+DateTime.Now.Millisecond.ToString();
string d=Server.MapPath("upfiles/")+s+filename ; //设置文件名
FileUp.PostedFile.SaveAs(d); //保存文件
Body.Value+="<img src=upfiles/"+s+filename+">"; //更改新闻内容
Label1.Text="上传成功";
Label2.Text="upfiles/"+s+filename;
}
else Label1.Text="只能上传图形文件";
}
else Label1.Text="请选择上传文件";
}