using System;
using System.Data;
using System.Configuration;
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;
using System.Text;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
//获取文件的名称;它与 this.FileUpload1.PostedFile.FileName所得到的值(全部路径)是不同的,
string filename = this.FileUpload1.FileName;
// Response.Write(filename + this.FileUpload1.PostedFile.FileName);
//把文件名的扩展名提取出来
string strextension = System.IO.Path.GetExtension(filename).ToLower();
//定义两个数组,分别初始化各包含所有图片和文件的扩展名
string[] extensions ={ ".jpg", ".png", "jif", ".bmp" };
string[] txtextensions ={ ".txt", ".doc" };
//判断所上传的文件是否是文件
for (int i = 0; i < extensions.Length; i++)
{
if (strextension.ToString() == extensions[i].ToString())
{
//判断所上传的文件的大小是否大于限定的值
if (this.FileUpload1.PostedFile.ContentLength < 4096)
{
this.FileUpload1.SaveAs(Server.MapPath("upload/" + filename));
//在image控件呈现出来
this.Image1.ImageUrl = "upload/" + filename;
}
else
{
RegisterStartupScript("", "<script>alert('wrong')</script>");
}
}
}
//判断所上传的文件是否是图片
for (int i = 0; i < txtextensions.Length; i++)
{
if (strextension.ToString() == txtextensions[i].ToString())
{
//判断所上传的文件的大小是否大于限定的值
if (this.FileUpload1.PostedFile.ContentLength < 4096)
{
this.FileUpload1.SaveAs(Server.MapPath("uploadfile/" + filename));
this.Image1.ImageUrl = "uploadfile/" + filename;
showFie(this.Request.MapPath("uploadfile/" + filename));
}
else
{
RegisterStartupScript("", "<script>alert('wrong')</script>");
}
}
}
}
//读取文件
public void showFie(string strFileCont)
{
this.ListBox1.Items.Clear();
//得到上传文件的名称
try
{
if (File.Exists(strFileCont))
{
//构件一个
FileStream fs = new FileStream(strFileCont, FileMode.Open);
//构件byte对象
byte[] arr = new byte[1000];
UTF8Encoding data = new UTF8Encoding();
while (fs.Read(arr, 0, arr.Length) > 0)
{
this.ListBox1.Items.Add(data.GetString(arr));
}
fs.Flush();
fs.Close();
}
else
{
this.Response.Write("<script>alert('文件不存在')</script>");
}
}
catch (Exception ex)
{
this.Response.Write("<script>alert('" + ex.Message + "')</script>");
}
finally
{
}
}
}