62,072
社区成员
发帖
与我相关
我的任务
分享
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
if (this.uploadfile.PostedFile.FileName.Equals("") || Request["formname"] == null || Request["editname"] == null || Request["uppath"] == null)
{
return;
}
else
{
string _formname = Request["formname"];
string _editname = Request["editname"];
string _uppath = Request["uppath"];
string _filelx = Request["filelx"];
string _regex = "";
//Regex test = new Regex(_regex);
Regex test = new Regex(@"^.+\.(rar)$");
string extension = System.IO.Path.GetExtension(uploadfile.FileName);
//if (test.IsMatch(uploadfile.PostedFile.FileName) == true )
//{
if (extension == ".rar" || extension == ".doc" || extension == ".xls" || extension=="zip")
{
ClientScriptManager cs = Page.ClientScript;
string imagesfolder = _uppath;
string filename = UpLoadFile(uploadfile, imagesfolder);
string apurl = Request.Url.ToString();
string apurl2 = Request.CurrentExecutionFilePath;
string tempurl = apurl.Substring(0, apurl.IndexOf(apurl2));
string appurl = Request.ApplicationPath;
string hurl = "";
if (appurl.Length == 1)
hurl = /*appurl + */filename.Replace("~/", "");
else
hurl = /*appurl + "/" +*/ filename.Replace("~/", "");
string imgpreview = tempurl + hurl;
cs.RegisterClientScriptBlock(this.GetType(), "tt", "window.opener.document." + _formname + "." + _editname + ".value='" + hurl.Replace("../", "") + "';", true);
cs.RegisterClientScriptBlock(this.GetType(), "tt2", "window.alert('文件上传成功!请不要修改生成的链接地址!');window.close();", true);
lblinfo.Text = "文件上传成功!<a href=\"\">重新上传</a>";
}
else
{
lblinfo.Text = "只能上传doc,xls,rar,zip!<a href=\"\">重新上传</a><script>parent.document.all('divProcessing').style.display='none';</script>";
}
//else
//{
// lblinfo.Text = "您上传的文件类型不正确!<a href=\"\">重新上传</a><script>parent.document.all('divProcessing').style.display='none';</script>";
//}
}
}
/// <summary>
/// 文件上传
/// </summary>
/// <param name="fileupload">文件上传实例</param>
/// <returns>保存的文件名称</returns>
public static string UpLoadFile(FileUpload fileupload, string Folders)
{
string fullname = fileupload.PostedFile.FileName;
if ((fullname == null) || (fullname.Equals("")))
return "";
string huozui = fullname.Substring(fullname.LastIndexOf("."));
string filename = GetFileName();
string p1 = Folders + filename + huozui;
//Folders + fullname.Substring(fullname.LastIndexOf("\\") + 1);
string path = System.Web.HttpContext.Current.Server.MapPath(p1);
if (System.IO.File.Exists(path))
System.IO.File.Delete(path);
fileupload.PostedFile.SaveAs(path);
return p1;
}
public static string GetFileName()
{
System.Threading.Thread.Sleep(1000);
string str1 = System.DateTime.Now.Year.ToString() + "-";
if ((System.DateTime.Now.Month).ToString().Length < 2)
{
str1 += "0" + System.DateTime.Now.Month.ToString() + "-";
}
else
{
str1 += System.DateTime.Now.Month.ToString() + "-";
}
if ((System.DateTime.Now.Day).ToString().Length < 2)
{
str1 += "0" + System.DateTime.Now.Day.ToString() + "-";
}
else
{
str1 += System.DateTime.Now.Day.ToString() + "-";
}
if ((System.DateTime.Now.Hour).ToString().Length < 2)
{
str1 += "0" + System.DateTime.Now.Hour.ToString() + "-";
}
else
{
str1 += System.DateTime.Now.Hour.ToString() + "-";
}
if ((System.DateTime.Now.Minute).ToString().Length < 2)
{
str1 += "0" + System.DateTime.Now.Minute.ToString() + "-";
}
else
{
str1 += System.DateTime.Now.Minute.ToString() + "-";
}
if ((System.DateTime.Now.Second).ToString().Length < 2)
{
str1 += "0" + System.DateTime.Now.Second.ToString();
}
else
{
str1 += System.DateTime.Now.Second.ToString();
}
return str1;
}
<form id="myform" runat="server" ><div id="esave" style="position:absolute; top:18px; left:40px; z-index:10; visibility:hidden">
<TABLE WIDTH=340 BORDER=0 CELLSPACING=0 CELLPADDING=0>
<TR><td width=20%></td>
<TD bgcolor=#104A7B width="60%">
<TABLE WIDTH=100% height=120 BORDER=0 CELLSPACING=1 CELLPADDING=0>
<TR>
<td bgcolor=#eeeeee align=center><font color=red>正在上传文件,请稍候...</font></td>
</tr>
</table>
</td><td width=20%></td>
</tr></table></div>
<table width="400" border="1" cellspacing="0" cellpadding="3" align="center" bordercolordark="#CCCCCC" bordercolorlight="#000000">
<tr bgcolor="#CCCCCC">
<td height="22" align="left" valign="middle" bgcolor="#CCCCCC" width="400"> 文件上传
</td>
</tr>
<asp:Label ID="lblinfo" runat="server">
<tr align="center" valign="middle">
<td align="left" id="upid" height="80" width="400"> 选择文件:
<asp:FileUpload ID="uploadfile" runat="server" Width="80%" /><br />
</td>
</tr>
<tr align="center" valign="middle" bgcolor="#eeeeee">
<td bgcolor="#eeeeee" height="24" width="400">
<asp:Button ID="ButUpload" runat="server" OnClick="Button1_Click" Text="· 开始上传 ·" CssClass="button" OnClientClick="javascript:mysub()" />
</td>
</tr>
</asp:Label>
</table>
</form>
//取得用户端上完整的档案名称
string strName = inputFile.PostedFile.FileName;
//取得文件名(抱括路径)里最后一个"."的索引 .doc .txt等后缀前的那个“.”
int intExt = strName.LastIndexOf(".");
//取得文件后缀名(扩展名)
string strExt = strName.Substring(intExt);
//根据日期和文件大小不同为文件命名,确保文件名不重复
DateTime datNow = DateTime.Now;
string strNewName1 = datNow.DayOfYear.ToString() + inputFile.PostedFile.ContentLength.ToString();// +strExt;