求一个文件上传的详细代码和注释。
需要上传文件到服务器指定文件夹,不能覆盖原有重名文件,并上传数个字段值(包括上传文件的路径和自定义字段值)到数据库,请各位大侠支招。(望保留注释,小弟学习)LANGUAGE="JAVASCRIPT"
需要上传文件到服务器指定文件夹,不能覆盖原有重名文件,并上传数个字段值(包括上传文件的路径和自定义字段值)到数据库,请各位大侠支招。(望保留注释,小弟学习)LANGUAGE="JAVASCRIPT"
//上传文件
public void FN_UpFiles()
{
//遍历File表单元素
HttpFileCollection files = HttpContext.Current.Request.Files;
//StringBuilder strMsg = new StringBuilder();
//strMsg.Append("上传的文件分别是:<hr color='pink'/>");
try
{
for (int iFile = 0 ; iFile < files.Count ; iFile++)
{
//检查文件扩展名字
HttpPostedFile postedFile = files[iFile];
string fileName = "";
//string fileExtension = "";
fileName = Path.GetFileName(postedFile.FileName);
if (fileName != "")
{
try
{
string strpath = HttpContext.Current.Request.MapPath("~/ResourcesFolder/") + fileName;
if (System.IO.File.Exists(strpath))
{
Response.Write("已经存在文件:" + fileName + "<br>");
}
else
{
try
{
NRModel.File model = new NRModel.File();
NRBLL.File bf = new NRBLL.File();
Guid guid1 = Guid.NewGuid();
Guid guid2 = Guid.NewGuid();
Guid guid3 = Guid.NewGuid();
Guid guid4 = Guid.NewGuid();
model.Fileid = guid1;
model.Folderid = guid2;
model.Filepath = strpath;
model.FileNam = fileName.ToString();
model.FileSize = postedFile.ContentLength;
model.Decription = this.decrition.Value;
model.CreateOn = DateTime.Now;
model.CreateBy = guid3;
model.ModefyBy = guid4;
if (bf.FN_AddNewRes(model) > 0)
{
//fileExtension = Path.GetExtension(fileName);
//strMsg.Append("上传的文件类型:" + postedFile.ContentType.ToString() + "<br>");
//strMsg.Append("客户端文件地址:" + postedFile.FileName + "<br>");
//strMsg.Append("上传文件的文件名:" + fileName + "<br>");
//strMsg.Append("上传文件的扩展名:" + fileExtension + "<br>");
//strMsg.Append("上传文件的大小:" + postedFile.ContentLength.ToString() + "个字节" + "<br>");
postedFile.SaveAs(System.Web.HttpContext.Current.Request.MapPath("~/ResourcesFolder/") + fileName);
Page.RegisterStartupScript("提示", "<script language='javascript'>alert('上传成功!')</script>");
Response.Write("<script language='javascript'>self.opener.location.reload();</script>");
Response.Write("<script language='javascript'>window.close();</script>");
}
}
catch (Exception ex)
{ Response.Write(ex.ToString());
} } }
catch (Exception ex)
{
Response.Write(ex.ToString());
}
}
else
{
Page.RegisterStartupScript("提示", "<script language='javascript'>alert('没有添加上传文件!')</script>");
}
}
//strStatus.Text = strMsg.ToString();
}
catch (System.Exception ex)
{
Response.Write(ex.ToString());
}
}看一下不懂的问
NRBLL.File bf = new NRBLL.File();
Guid guid1 = Guid.NewGuid();
Guid guid2 = Guid.NewGuid();
Guid guid3 = Guid.NewGuid();
Guid guid4 = Guid.NewGuid();
model.Fileid = guid1;
model.Folderid = guid2;
model.Filepath = strpath;
model.FileNam = fileName.ToString();
model.FileSize = postedFile.ContentLength;
model.Decription = this.decrition.Value;
model.CreateOn = DateTime.Now;
model.CreateBy = guid3;
model.ModefyBy = guid4;
if (bf.FN_AddNewRes(model) > 0)
{
//fileExtension = Path.GetExtension(fileName);
//strMsg.Append("上传的文件类型:" + postedFile.ContentType.ToString() + "<br>");
//strMsg.Append("客户端文件地址:" + postedFile.FileName + "<br>");
//strMsg.Append("上传文件的文件名:" + fileName + "<br>");
//strMsg.Append("上传文件的扩展名:" + fileExtension + "<br>");
//strMsg.Append("上传文件的大小:" + postedFile.ContentLength.ToString() + "个字节" + "<br>");
postedFile.SaveAs(System.Web.HttpContext.Current.Request.MapPath("~/ResourcesFolder/") + fileName);
Page.RegisterStartupScript("提示", "<script language='javascript'>alert('上传成功!')</script>");
Response.Write("<script language='javascript'>self.opener.location.reload();</script>");
Response.Write("<script language='javascript'>window.close();</script>");
}这个就是保存服务器文件路径到数据库的
http://www.cnblogs.com/oec2003/archive/2010/01/06/1640027.html
这个字段就是描述 你需要更多的描述 增加字段继续添加就可以了 如果你的意思只是说增加字段的问题的话
参考