博客园 首页 联系 订阅 管理
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="Enterprise.WebUI.WebForm1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="fileNameTextBox" runat="server" Columns="50" MaxLength="20" Width="262px" /></span><span><asp:Label ID="notefileNameLabel" runat="server" Text="*"></asp:Label></span>
<br />

<asp:FileUpload ID="FileUpload1" runat="server" Width="551px" /><asp:Label ID="lab" runat="server" Text="*"></asp:Label>

<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
</div>
</form>
</body>
</html>
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;

namespace Enterprise.WebUI
{
public partial class WebForm1 : System.Web.UI.Page
{
private static string path = null;
protected void Page_Load(object sender, EventArgs e)
{

}

protected void Button1_Click(object sender, EventArgs e)
{

if (UpLoadFile(this.FileUpload1))
{
string GoodsImg1 = path;
this.lab.Text = "<font color='red'>上传成功!</font>";
this.fileNameTextBox.Text = GoodsImg1;
}
else
{
this.lab.Text = "<font color='red'>上传失败!</font>";
}
}
/// <summary>
/// 上传图片
/// </summary>
/// <param name="fileUpload">控件名</param>
/// <param name="contentLength">图片大小,以k为单位</param>
/// <returns></returns>
public bool UpLoadFile(FileUpload fileUpload)
{
if (fileUpload.HasFile)
{
string fileName = FileUpload1.FileName;
string ext = Path.GetExtension(fileName);
if (this.FileUpload1.FileName != "")
{
if (ext == ".doc" || ext == ".docx" || ext == ".pdf" || ext == ".xlsx" || ext == ".xls" || ext == ".ppt" || ext == ".txt")
{

}
else
{
this.lab.Text = "上传的格式不正确。";
}
}
else
{
this.lab.Text = "请选择所要上传的文档。";
}
//文档大小
string filesize = (this.FileUpload1.PostedFile.ContentLength / 1024).ToString();
string paths = System.Web.HttpContext.Current.Server.MapPath(@"~\images\UploadFile\") + fileName;
//FileUpload1.SaveAs(Server.MapPath(path) + "\\" + fileName);
FileUpload1.SaveAs(paths);
path = ConfigurationManager.AppSettings["url"].ToString() + "images/homepro/" + fileName;

return true;
}
return false;
}
}
}



posted on 2011-12-01 11:56  $蔷  阅读(201)  评论(0编辑  收藏  举报