asp.net网站中上传文件代码
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
if (this.txtDescript.Text != "")
{
if (FileUpLoad1.HasFile)
{
//判断文件是否小于10Mb
if (FileUpLoad1.PostedFile.ContentLength < 10485760)
{
try
{
//上传文件并指定上传目录的路径
FileUpLoad1.PostedFile.SaveAs(Server.MapPath("~/UpLoad/")
+ FileUpLoad1.FileName);
/*注意->这里为什么不是:FileUpLoad1.PostedFile.FileName
* 而是:FileUpLoad1.FileName?
* 前者是获得客户端完整限定(客户端完整路径)名称
* 后者FileUpLoad1.FileName只获得文件名.
*/
//当然上传语句也可以这样写(貌似废话):
//FileUpLoad1.SaveAs(@"D:\"+FileUpLoad1.FileName);
//获取客户端要上传的文件路径
string fullfilename = this.FileUpLoad1.PostedFile.FileName;
//获取客户端要上传的文件名
string filename = fullfilename.Substring(fullfilename.LastIndexOf("\\") + 1);
//文件的相对路径
string RelativePath = "\\UpLoad\\" + filename;
//绝对路径
//operation.InsertSoft(ddlTitle.SelectedItem.ToString().Trim(),filename,fullfilename, txtDescript.Text.ToString().Trim(),false,DateTime.Now.ToString());
//相对路径
operation.InsertSoft(ddlTitle.SelectedItem.ToString().Trim(), filename, RelativePath, txtDescript.Text.ToString().Trim(), false, DateTime.Now.ToString());
//lblMessage.Text = "上传成功!";
Page.ClientScript.RegisterStartupScript(Page.GetType(), "", "<script>if(confirm('上传成功!是否继续上传?')){location.href='UpLoad.aspx'}else{location.href='Welcome.aspx'}</script>");
}
catch (Exception ex)
{
lblMessage.Text = "出现异常,无法上传!";
lblMessage.Text += ex.Message;
}
}
else
{
lblMessage.Text = "上传文件不能大于10MB!";
}
}
else
{
lblMessage.Text = "尚未选择文件!";
}
}
else
{
lblMessage.Text = "文件描述不能为空!";
}