this
:
比如前台有3个INPUT:
然后后台:
HttpFileCollection
files
=
HttpContext.Current.Request.Files
//这个files里面就是你上传文件的集合。遍历即可。
FileUpload实现单图片上传,如果想多图片上传,你试试这个:<tr>
<td align="right" valign="top">
试卷照片:
</td>
<td align="left">
<div id="_container">
<input id="File1" type="file" name="File" runat="server" size="10" />
</div>
</td>
<td align="left" valign="bottom">
<input type="button" value="添加" onclick="addFile()" />
</td>
</tr>
addFile()源码:
//多文件上传,动态生成多个上传控件
function addFile() {
var div = document.createElement("div")
var f = document.createElement("input")
f.setAttribute("type", "file")
f.setAttribute("name", "file")
f.setAttribute("size", "10")
div.appendChild(f)
document.getElementById("_container").appendChild(div)
}
后台页面调用:
#region 上传添加图片的方法
/// <summary>
/// 上传添加图片的方法
/// </summary>
/// <param name="nId">关联id</param>
private static void UploadAndAddPicTures(int nId)
{
LMS.BLL.TRAIN_Pictrue PictrueBLL = new LMS.BLL.TRAIN_Pictrue()
List<LMS.Model.TRAIN_Pictrue>list = new List<LMS.Model.TRAIN_Pictrue>()
//遍历File表单元素
HttpFileCollection files = HttpContext.Current.Request.Files
for (int iFile = 0iFile <files.CountiFile++)
{
//检查文件扩展名字
HttpPostedFile postedFile = files[iFile]
string fileName
fileName = System.IO.Path.GetFileName(postedFile.FileName)
if (fileName.ToLower() != "")
{
LMS.Model.TRAIN_Pictrue Pictrue = new LMS.Model.TRAIN_Pictrue()
string scurTypeName = fileName.Substring(fileName.LastIndexOf("."))
string sGuid_phy = Guid.NewGuid().ToString()
string sUrl_phy = ConfigurationManager.AppSettings["PhysicsObjectPath"].ToString() + sGuid_phy + scurTypeName
//初始化缩略图物理路径
string sGuid_web = Guid.NewGuid().ToString()
string sUrl_web = ConfigurationManager.AppSettings["PhysicsObjectPath"].ToString() + sGuid_web + scurTypeName
postedFile.SaveAs(sUrl_phy)//保存原图
PTImage.ZoomAuto(postedFile, sUrl_web, 100, 100, "", "")//生成缩略图,并保存
//保存原图虚拟路径到数据库
Pictrue.path = ConfigurationManager.AppSettings["WebObjectPath"].ToString() + sGuid_phy + scurTypeName
//保存缩略图虚拟路径到数据库
Pictrue.shrinkpath = ConfigurationManager.AppSettings["WebObjectPath"].ToString() + sGuid_web + scurTypeName
Pictrue.parid = nId
Pictrue.tables = "TRAIN_Hotel_MonthExam"
list.Add(Pictrue)
}
}
PictrueBLL.Add(list)
}
#endregion
希望对你有帮助!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)