前台做了一个多文件上传功能 页面上放置了一个HTML的按钮控件
后台有一个函数public void Upfile()
{
//遍历File表单元素
HttpFileCollection files = HttpContext.Current.Request.Files;
//状态信息
StringBuilder strMsg = new StringBuilder();
strMsg.Append("上传的文件分别是:<hr color='red'/>");
try
{
for (int iFile = 0; iFile < files.Count; iFile++)
{
//检查文件扩展名字
HttpPostedFile postedFile = files[iFile];
string fileName, fileExtension;
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>");
return;
}
else
{
Model.File mf = new Model.File();
BLL.NRBLL bn = new BLL.NRBLL();
Guid guid1 = Guid.NewGuid();
Guid guid2 = Guid.NewGuid();
Guid guid3 = Guid.NewGuid();
Guid guid4 = Guid.NewGuid();
mf.Fileid = guid1;
mf.Folderid = guid2;
mf.Filepath = strpath;
mf.FileSize = postedFile.ContentLength;
mf.Decription = postedFile.ContentType.ToString();
mf.CreateOn = DateTime.Now;
mf.CreateBy = guid3;
mf.ModefyOn = DateTime.Now;
mf.ModefyBy = guid4;
if (bn.FN_UploadRes(mf) > 0)
{
Response.Write("文件上传成功:" + fileName + "<br>");
}
else
{
Response.Write("文件上传失败:" + fileName + "<br>");
}
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);
} }
catch (Exception)
{ throw;
} }
} strStatus.Text = strMsg.ToString();
}
catch (System.Exception Ex)
{
strStatus.Text = Ex.Message;
}
}
那么我前台的这个HTML按钮如何触发它的onclick事件呢?我用WEB控件试过了 很容易 但是公司不允许用WEB控件
如果用ajaxpro可以吗 这个是没有参数的
后台有一个函数public void Upfile()
{
//遍历File表单元素
HttpFileCollection files = HttpContext.Current.Request.Files;
//状态信息
StringBuilder strMsg = new StringBuilder();
strMsg.Append("上传的文件分别是:<hr color='red'/>");
try
{
for (int iFile = 0; iFile < files.Count; iFile++)
{
//检查文件扩展名字
HttpPostedFile postedFile = files[iFile];
string fileName, fileExtension;
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>");
return;
}
else
{
Model.File mf = new Model.File();
BLL.NRBLL bn = new BLL.NRBLL();
Guid guid1 = Guid.NewGuid();
Guid guid2 = Guid.NewGuid();
Guid guid3 = Guid.NewGuid();
Guid guid4 = Guid.NewGuid();
mf.Fileid = guid1;
mf.Folderid = guid2;
mf.Filepath = strpath;
mf.FileSize = postedFile.ContentLength;
mf.Decription = postedFile.ContentType.ToString();
mf.CreateOn = DateTime.Now;
mf.CreateBy = guid3;
mf.ModefyOn = DateTime.Now;
mf.ModefyBy = guid4;
if (bn.FN_UploadRes(mf) > 0)
{
Response.Write("文件上传成功:" + fileName + "<br>");
}
else
{
Response.Write("文件上传失败:" + fileName + "<br>");
}
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);
} }
catch (Exception)
{ throw;
} }
} strStatus.Text = strMsg.ToString();
}
catch (System.Exception Ex)
{
strStatus.Text = Ex.Message;
}
}
那么我前台的这个HTML按钮如何触发它的onclick事件呢?我用WEB控件试过了 很容易 但是公司不允许用WEB控件
如果用ajaxpro可以吗 这个是没有参数的
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货