62,041
社区成员
发帖
与我相关
我的任务
分享
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections;//引入数组的命名空间
public partial class index : System.Web.UI.Page
{
public string PageTitle;
protected void Page_Load(object sender, EventArgs e)
{
PageTitle = "上传文件";
ImgDisp.Visible = false;
}
protected void BtnUpLoad_Click(object sender, EventArgs e)
{
string oHtmlNow = DateTime.Now.ToString("yyyyMMddHHmmssffHmd");
string oFileName = FileUpLoad.FileName;//获取文件名称
string oFileSize = FileUpLoad.PostedFile.ContentLength.ToString();//获取文件大小
string oFileType = FileUpLoad.PostedFile.ContentType;//获取文件类型
string oFileIndex = oFileName.Substring(oFileName.LastIndexOf(".") + 1);//获取文件后缀
string oImagesUpLoad = Server.MapPath("UpLoadPic\\") + oHtmlNow + "." + oFileIndex;
string oFileUpLoad = Server.MapPath("UpLoadFile\\") + oHtmlNow + "." + oFileIndex;
string oFileImagesPath = "UpLoadPic/" + oHtmlNow + "." + oFileIndex;
string oFilePath = "UpLoadFile/" + oHtmlNow + "." + oFileIndex;
//开始判断上传类型
if (oFileIndex == "jpg" || oFileIndex == "gif" || oFileIndex == "png" || oFileIndex == "bmp")
{
ImgDisp.Visible = true;
FileUpLoad.SaveAs(oImagesUpLoad);
ImgDisp.ImageUrl = oFileImagesPath;
LabTxt.Text = "文件名为:" + oFileImagesPath + ",文件大小为:" + oFileSize + ",文件类型为:" + oFileType + ",文件后缀为:" + oFileIndex + "";
}
else
{
ImgDisp.Visible = false;
FileUpLoad.SaveAs(oFileUpLoad);
LabTxt.Text = "文件名为:" + oFilePath + ",文件大小为:" + oFileSize + ",文件类型为:" + oFileType + ",文件后缀为:" + oFileIndex + "";
}
}
}
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="index.aspx.cs" Inherits="index" %>
<!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><%=PageTitle %></title>
</head>
<body>
<form id="form1" runat="server">
<asp:TextBox ID="TxtUpLoad" runat="server" ReadOnly="True" Width="250px"></asp:TextBox>返回到这个文本框中
<br />
<asp:FileUpload ID="FileUpLoad" runat="server" />
<asp:Button ID="BtnUpLoad" runat="server" OnClick="BtnUpLoad_Click" Text="上传文件" />
<br />
<asp:Image ID="ImgDisp" runat="server" />
<br />
<asp:Label ID="LabTxt" runat="server"></asp:Label>
</form>
</body>
</html>