protected void btnUp_Click(object sender, EventArgs e)
{
string savePath = @"images/";//图片保存路径
string fileName = Server.HtmlEncode(picupload.FileName);
string extension = System.IO.Path.GetExtension(fileName).ToLower();//取得扩展名
string ImageName = "";
String[] allowExtension = { ".jpg", ".jif", ".jpeg", ".jpe", ".png", ".bmp" };
for (int i = 0; i < allowExtension.Length; i++)
{
if (extension == allowExtension[i] && fileName != "nopic.jpg")
{
ImageName = DateTime.Now.ToString("yyyyMMddhhmmssfff") + extension;
savePath += ImageName;
savePath = Server.MapPath(savePath);
picupload.SaveAs(savePath);
Response.Write("<script>alert('图片上传成功'),location.href='Test.aspx'</script>");
}
else
{
Label1.Text = "请选择jpg, jif, jpeg, jpe, png, bmp格式的图片";
}
}
#region 文件上传
String Year = DateTime.Now.Year.ToString();
String Month = DateTime.Now.Month.ToString();
String Day = DateTime.Now.Day.ToString();
String Time = Year + Month + Day;
//创建文件夹的绝对路径
String Path = @"D:搜狗高速下载ShopShopAdminDownload" + Time;
//上传文件的路径
String savePath = @"Download\" + Time + "\";
//判断是否存在这个文件夹
if (!Directory.Exists(Path))
{
//创建文件夹
Directory.CreateDirectory(Path);
savePath = File(savePath);
}
else
{
savePath = File(savePath);
}
#endregion
private String File(String savePath)
{
//文件保存路径
string fileName = Server.HtmlEncode(FileUpload1.FileName);
string extension = System.IO.Path.GetExtension(fileName).ToLower();//取得扩展名
string ImageName = "";
String[] allowExtension = { ".txt", ".docx", ".xls", ".pptx", ".xlsx", ".doc" };
for (int i = 0; i < allowExtension.Length; i++)
{
if (extension == allowExtension[i])
{
ImageName = fileName;
savePath += ImageName;
savePath = Server.MapPath(savePath);
FileUpload1.SaveAs(savePath);
HiddenField1.Value = savePath;
Label1.Text = "";
}
else
{
if (HiddenField1.Value == "")
{
Label1.Text = "请选择txt, docx, xls, pptx, xlsx, doc格式的文件";
}
}
}
return savePath;
}
//创建文件夹的绝对路径
String Path = @"D:搜狗高速下载ShopShopAdminDownload" + Time;
//上传文件的路径
String savePath = @"Download\" + Time + "\";
//判断是否存在这个文件夹
if (!Directory.Exists(Path))
{
Directory.CreateDirectory(Path);
//创建文件夹
savePath = File(savePath);
}
else
{
savePath = File(savePath);
}
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:Button ID="btnUp" runat="server" Text="上传图片" OnClick="btnUp_Click" />
<asp:FileUpload ID="picupload" runat="server" />
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Label ID="Label1" runat="server" ForeColor="Red"></asp:Label>
</ContentTemplate>
</asp:UpdatePanel>
<img src="images/20140915024843589.jpg" width="100px" />
</div>