62,074
社区成员
发帖
与我相关
我的任务
分享
HttpFileCollection files = HttpContext.Current.Request.Files;
if (files.Count <= 1)
{
Page.ClientScript.RegisterClientScriptBlock(GetType(), "", "<script>alert('请添加图片附件!'); hideWaitLayer();</script>");
}
else
{
try
{
for (int i = 0; i < files.Count; i++)
{
HttpPostedFile postedFile = files[i];
string filepath = System.IO.Path.GetFileName(postedFile.FileName);//获取文件名
if (filepath != "")
{
string fileName = filepath.Substring(filepath.LastIndexOf("\\") + 1);
string fileExtensions = fileName.Substring(fileName.LastIndexOf(".") + 1);//获取后缀名
//避免一次上传多个造成命名重复,特此再时间后再加一个序号,考虑到用户量不会很大,更大的并发操作在这里不考虑
string newFileName = DateTime.Now.ToString("yyyyMMddHHmmss") + i + "." + fileExtensions;
string serverPath = Server.MapPath("../Adjunct/ImagesNews/" + newFileName);
postedFile.SaveAs(serverPath);
ImagesNews.InsertImagesNews(txtName.Text == "" ? fileName : txtName.Text, txtDesc.Text, newFileName, "");//Session["userName"].ToString()
}
}
Page.ClientScript.RegisterClientScriptBlock(GetType(), "", "<script>alert('添加完成!');hideWaitLayer(); window.close(); </script>"); //window.opener.location.href=window.opener.location.href; window.reload();
}
catch (Exception ex)
{
throw ex;
Page.ClientScript.RegisterClientScriptBlock(GetType(), "", "<script>alert('添加失败!'); hideWaitLayer();</script>");
}
}