我在网上搜
发现了
HtmlInputFile控件 加
HtmlInputFileControl这个类 上传的方法结果是完全看不懂在.net里 也没有HtmlInputFile
只有fileupload 这个是用于上传的吧顺便问句
#region HtmlInputFileControl这样的定义 是定义什么
发现了
HtmlInputFile控件 加
HtmlInputFileControl这个类 上传的方法结果是完全看不懂在.net里 也没有HtmlInputFile
只有fileupload 这个是用于上传的吧顺便问句
#region HtmlInputFileControl这样的定义 是定义什么
//代码折叠
public class MyClass
{
static void Main()
{
}
}
#endregionhtmlfileupload的方法要自己写!!
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default6.aspx.cs" Inherits="_Default" %>
<%@ Import Namespace = "System.IO" %>
<html>
<head>
<title>上传文件</title>
</head>
<body>
<div align="center"><table width="100%" border="0" cellpadding="0" cellspacing="0" style="border-collapse: collapse" bordercolor="#eeeeee" id="AutoNumber1">
<form id="formFile" method="post" action="" enctype="multipart/form-data" runat="server">
<tr>
<td align="center" style="height: 22px">
<input type="file" id="inputFile" name="inputFile" runat="server" size="64">
</td>
</tr>
<tr>
<td align="center">
<input type="button" value="Upload" OnServerClick="uploadFile" id="Button2" name="Button1" runat="server">
</td>
</tr>
<tr>
<td align="center">
<asp:Label id="labelUpResult" runat="server" text="" font-bold="True" forecolor="#FF0000"/><br>
<asp:Label id="labelFileName" runat="server" text="" font-bold="True" forecolor="#FF0000"/><br>
<asp:Label id="labelFileExt" runat="server" text="" font-bold="True" forecolor="#FF0000"/><br>
<asp:Label id="labelFileSize" runat="server" text="" font-bold="True" forecolor="#FF0000"/><br>
<asp:GridView ID="GridView1" runat="server" Height="182px" Width="799px">
</asp:GridView>
</td>
</tr>
</form>
</table>
</div>
</body>
</html>这是后台控制using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.OleDb;
using System.IO;public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{ } public void uploadFile(object sender, EventArgs E)
{
//检查上传文件不为空
if (inputFile.PostedFile.ContentLength > 0)
{
//设定上传文件的保存路径
string strSaveDir = "./Upload/";
string strName = inputFile.PostedFile.FileName;
//取得文件名(抱括路径)里最后一个"."的索引
int intExt = strName.LastIndexOf(".");
//取得文件扩展名
string strExt = strName.Substring(intExt);
//这里我自动根据日期和文件大小不同为文件命名,确保文件名不重复
/*
DateTime datNow = DateTime.Now;
string strNewName = datNow.DayOfYear.ToString() + inputFile.PostedFile.ContentLength.ToString() + strExt; */
//取得文件名(包括路径)里最后一个"\"的索引
int intPath = strName.LastIndexOf("\\");
//取得文件名(不包括路径)
string strNewName = strName.Substring(intPath);
//保存文件到你所要的目录,这里是IIS根目录下的upload目录.你可以改变.
//注意: 我这里用Server.MapPath()取当前文件的绝对目录.在asp.net里"\"必须用"\\"代替
inputFile.PostedFile.SaveAs(Server.MapPath(strSaveDir + strNewName));
//得到这个文件的相关属性:文件名,文件类型,文件大小
this.GridView1.DataSource = ExcelDataSource(Server.MapPath(strSaveDir + strNewName), "Sheet1");
this.GridView1.DataBind();
labelUpResult.Text = "上传成功!";
labelFileName.Text = "文件源:" + strName;
labelFileExt.Text = "文件类型:" + inputFile.PostedFile.ContentType + "( " + strExt + " )";
labelFileSize.Text = "文件大小:" + (inputFile.PostedFile.ContentLength / 1024).ToString() + " K Byte(s)";
}
else
{
labelUpResult.Text = "请选择你要上传的文件!";
labelFileName.Text = "";
labelFileExt.Text = "";
labelFileSize.Text = "";
}
}
//该方法实现从Excel中导出数据到DataSet中,其中filepath为Excel文件的绝对路径,sheetname为表示那个Excel表;
public DataSet ExcelDataSource(string filepath, string sheetname)
{
string strConn;
strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filepath + ";Extended Properties=Excel 8.0;";
OleDbConnection conn = new OleDbConnection(strConn);
OleDbDataAdapter oada = new OleDbDataAdapter("select * from [" + sheetname + "$]", strConn);
DataSet ds = new DataSet();
oada.Fill(ds);
return ds;
}
}