日期:2014-05-17  浏览次数:20497 次

求点击按钮,自动浏览文件,选好文件之后自动上传!
fileupload 是隐藏的。前两天做了测试已经可以了,但是今天不知道怎么搞的,出现了问题:点击按钮弹出选择文件对话框,但是选完文件之后却不能自动上传!求各位大侠解答!!!
HTML代码
HTML code

<%@ 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>



CS代码:
C# code

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;   
        }
             
    }
}




------解决方案--------------------
留QQ 使用Jquary插件吧。 可以自己先看看,百度或者www.uploadify.com
------解决方案--------------------
你选择的浏览器 确定openBrowse的点击事件 执行了?
------解决方案--------------------
恩 同意楼上的用插件吧 自己来写太浪费时间了