110,536
社区成员
发帖
与我相关
我的任务
分享
int i = 1;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
SFUPC();
}
}
private void SFUPC()//该方法用于保存当前页面上传文件控件集到缓存中
{
ArrayList AL = new ArrayList();//创建动态增加数组
foreach (Control C in F.Controls)
{
if (C.GetType().ToString() == "System.Web.UI.HtmlControls.HtmlTableRow")
{
HtmlTableCell HTC = (HtmlTableCell)C.Controls[0];
foreach (Control FUC in HTC.Controls)
{
if (FUC.GetType().ToString() == "System.Web.UI.WebControls.FileUpload")
{
System.Web.UI.WebControls.FileUpload FU = (System.Web.UI.WebControls.FileUpload)FUC;
AL.Add(FU);
}
}
}
}
Session.Add("FilesControls", AL);
}
private void InsertC()//该方法用于添加一个上传文件的控件
{
ArrayList AL = new ArrayList();
this.F.Rows.Clear();
GetInfo();
HtmlTableRow HTR = new HtmlTableRow();
HtmlTableCell HTC = new HtmlTableCell();
HTC.Controls.Add(new FileUpload());
HTR.Controls.Add(HTC);
this.F.Controls.Add(HTR);
SFUPC();
i = F.Rows.Count;
}
private void GetInfo()//该方法用于读取缓存中存储的上传文件控件集
{
ArrayList AL = new ArrayList();
if (Session["FilesControls"] != null)
{
AL = (System.Collections.ArrayList)Session["FilesControls"];
for (int i = 0; i < AL.Count; i++)
{
HtmlTableRow HTR = new HtmlTableRow();
HtmlTableCell HTC = new HtmlTableCell();
HTC.Controls.Add((System.Web.UI.WebControls.FileUpload)AL[i]);
HTR.Controls.Add(HTC);
this.F.Rows.Add(HTR);
}
}
}
private void UpFile()//该方法用于执行文件上传操作
{
string FilePath = Server.MapPath("./") + "File";
HttpFileCollection HFC = Request.Files;
for (int i = 0; i < HFC.Count; i++)
{
HttpPostedFile UserHPF = HFC[i];
try
{
if (UserHPF.ContentLength > 0)
{
UserHPF.SaveAs(FilePath +"\\" + System.IO.Path.GetFileName(UserHPF.FileName));
}
}
catch
{
lbl_message.Text = "处理出错!";
}
}
if (Session["FilesControls"] != null)
{
Session.Remove("FilesControls");
}
lbl_message.Text = "上传成功!";
}
js :code
<script language="JavaScript" type="text/javascript">
function addFile() {
var str = "<br/><input type='file' style='width:300px' name='File' />"
document.getElementById('PnImage').insertAdjacentHTML("beforeEnd", str)
}
</script>
html:Code
<asp:Panel ID="PnImage" runat="server">
<asp:FileUpload ID="fileupload" runat="server" Width="300px" />
</asp:Panel>
<asp:Label ID="lblPicture" runat="server" Text="(默认情况下图片:qq.gif)<a href='javascript:void(0)' onclick='addFile()'>添加一张</a> "></asp:Label>
后台代码:
//开始上传
protected void btnSave_Click(object sender, EventArgs e)
{
try
{
HttpFileCollection files = HttpContext.Current.Request.Files;
for (int iFile = 0; iFile < files.Count; iFile++)
{
///'检查文件扩展名字
HttpPostedFile postedFile = files[iFile];
string strPath = postedFile.FileName;
int index = strPath.LastIndexOf(".");
int indexc = strPath.LastIndexOf("\\");
//获取上传文件扩展名
string strExtends = strPath.Substring(index);
//32位Guid,不可能一样的值
string strName = strPath.Substring(indexc);
this.fileupload.PostedFile.SaveAs(Server.MapPath("../Upload/FCKEditor/image/") + strName);
//存入数据库相对路径
//model.Photos = strName;
// Response.Write(postedFile.FileName);
postedFile.SaveAs(Server.MapPath("../Upload/FCKEditor/image/") + strName);
}
Response.Write("<script>alert('操作成功')</script>");
// ClientScript.RegisterStartupScript(typeof(string), "hash", "$.prompt('操作成功',{opacity: 0.2})", true);
}
catch (Exception ex)
{
Response.Write("<script>alert(" + ex.Message + ")</script>");
}
}