实现多文件上传 将孟子E章得代码稍微修改了一下
现在的问题是 他的代码只能靠Web按钮的点击事件来实现
我现在想用html控件 想在前台调用后台函数 关键这整个函数必须返回string 类型那么我整个函数该如何返回?
//多文件上传
public void FN_UpFiles()
{
//遍历File表单元素
HttpFileCollection files = HttpContext.Current.Request.Files;
try
{
for (int iFile = 0 ; iFile < files.Count ; iFile++)
{
//检查文件扩展名字
HttpPostedFile postedFile = files[iFile];
string fileName = "";
fileName = Path.GetFileName(postedFile.FileName);
if (fileName != "")
{
try
{
string strpath = System.Web.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)
{
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());
}
}
}
}
catch (System.Exception ex)
{
Response.Write(ex.ToString());
}
}
现在的问题是 他的代码只能靠Web按钮的点击事件来实现
我现在想用html控件 想在前台调用后台函数 关键这整个函数必须返回string 类型那么我整个函数该如何返回?
//多文件上传
public void FN_UpFiles()
{
//遍历File表单元素
HttpFileCollection files = HttpContext.Current.Request.Files;
try
{
for (int iFile = 0 ; iFile < files.Count ; iFile++)
{
//检查文件扩展名字
HttpPostedFile postedFile = files[iFile];
string fileName = "";
fileName = Path.GetFileName(postedFile.FileName);
if (fileName != "")
{
try
{
string strpath = System.Web.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)
{
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());
}
}
}
}
catch (System.Exception ex)
{
Response.Write(ex.ToString());
}
}
服务器控件,只是 asp.net 给你包装好的html控件
action="uploadfile.aspx"
获取所有上传文件 Request.Files
你的意思是用submit表单提交的方式?