用FileUpload控件怎么实现多图片上传
答案:2 悬赏:0 手机版
解决时间 2021-02-13 17:33
- 提问者网友:暗中人
- 2021-02-13 11:11
用FileUpload控件怎么实现多图片上传
最佳答案
- 五星知识达人网友:醉吻情书
- 2021-02-13 12:51
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 = 0; iFile < files.Count; iFile++)
{
//检查文件扩展名字
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
希望对你有帮助!
<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 = 0; iFile < files.Count; iFile++)
{
//检查文件扩展名字
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
希望对你有帮助!
全部回答
- 1楼网友:执傲
- 2021-02-13 13:59
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 = 0; ifile < files.count; ifile++)
{
//检查文件扩展名字
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
希望对你有帮助!
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯
正方形一边上任一点到这个正方形两条对角线的 |
阴历怎么看 ? |