日期:2014-05-17 浏览次数:20497 次
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %> <!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> <script type="text/javascript"> function openBrowse() { var ie=navigator.appName=="Microsoft Internet Explorer" ? true : false; if(ie) { document.getElementById("FileUpload1").click(); //document.getElementById("filename").value=document.getElementById("file").value; } else { var a=document.createEvent("MouseEvents");//FF的处理 a.initEvent("click", true, true); document.getElementById("FileUpload1").dispatchEvent(a); } } </script> </head> <body> <form id="form1" runat="server" action="http://localhost/"> <div> <asp:FileUpload ID="FileUpload1" onchange="javascript:__doPostBack('UploadDocument','')" runat="server" ToolTip="选择文件"/> <asp:LinkButton ID="UploadDocument" runat="server" OnClick="UploadDocument_Click"></asp:LinkButton> <br /> <input type="button" name="button" value="点我就像点击“浏览”按钮一样" onclick="javascript:openBrowse();" /> </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.Data.SqlClient; public partial class Default2 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { FileUpload1.Attributes.Add("style", "display:none"); } protected void UploadDocument_Click(object sender, EventArgs e) { if (FileUpload1.PostedFile != null && FileUpload1.PostedFile.ContentLength > 0) { string ext = System.IO.Path.GetExtension(FileUpload1.PostedFile.FileName).ToLower(); if (ext != ".doc" && ext != ".docx" && ext != ".txt") return; string filename = FileUpload1.FileName; string path = "~/Files/" + filename; FileUpload1.PostedFile.SaveAs(Server.MapPath(path)); } else { //do some thing; } } }