//文件名
        Response.Write(System.IO.Path.GetFileName(this.File1.PostedFile.FileName));
        //文件扩展名
        Response.Write(System.IO.Path.GetExtension(this.File1.PostedFile.FileName));
        //文件全路径
        Response.Write(this.File1.PostedFile.FileName);
        //文件类型
        Response.Write(this.File1.PostedFile.ContentType);多谢Sandy945 告诉我这个处理方法,现在的确解决了,但新的问题又出来了.点一个button时可以将选择的文件名称显示到label上,但若将button放到updatepanel或者table里面,再点就抱错了,我想应该是this的问题,可C#又没有parent这个保留字,请问要怎样处理,谢谢!

解决方案 »

  1.   

    Response.Write在UpdatePanel中应该不能用吧
      

  2.   

    在UPdatepanel的triggers添加PostBackTriggers,将PostBackTriggers的ControlID设置button的ID.
    这样在按钮的单击事件里就可以使用 Response.Write(this.File1.PostedFile.ContentType); 
    但是这样页面会刷新.
      

  3.   

    是这样的,页面上有一个Input File,一个UpdatePanel和一个Button,Button放在UpdatePanel里面
    protected void Button1_Click(object sender, EventArgs e)
        {
            string sFileName;
            sFileName = System.IO.Path.GetFileName(this.File1.PostedFile.FileName);
        }如果Button放在UpdatePanel,就可以得到文件名称,放在里面就报错了,不光是UpdatePanel不可以,其它的如Table,Div都不可以的,Button必须放在最外层,所以我觉得这里是不是不应该用this ?
      

  4.   


    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="FileUpload.aspx.cs" Inherits="FileUpload" %><!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>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <input type="file" id="File1" size="40" name="File1"  runat="server"  />
            <asp:FileUpload ID="FileUpload1" runat="server" />
        </div>
            <input type="file" id="File2" size="40" name="File2"  runat="server"  />    
        <div >
            <asp:Button ID="btn" runat="server" Text="上传" OnClick="btn_Click" Width="50px"  />
        </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;public partial class FileUpload : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {    }
        protected void btn_Click(object sender, EventArgs e)
        {
            Response.Write("页面中FileUpload的个数" + HttpContext.Current.Request.Files.Count + "<br>");        //HttpContext.Current.Request.Files 当前请求对象的file集合
            //文件名
            Response.Write("div 内的 File1 的文件名 " + System.IO.Path.GetFileName(HttpContext.Current.Request.Files[0].FileName) + "<br>");
            //文件扩展名
            Response.Write("div 内的 File1 的文件扩展名 " + System.IO.Path.GetExtension(HttpContext.Current.Request.Files[0].FileName) + "<br>");       
            //文件全路径
            Response.Write("div 内的 File1 的文件全路径 " + HttpContext.Current.Request.Files[0].FileName + "<br>");
            //文件类型
            Response.Write("div 内的 File1 的文件类型 " + HttpContext.Current.Request.Files[0].ContentType + "<br>");
            //文件大小
            Response.Write("div 内的 File1 的文件大小 " + HttpContext.Current.Request.Files[0].ContentLength + "<br>");
            //文件名
            Response.Write("div 内的 FileUpload1 的文件名 " + System.IO.Path.GetFileName(FileUpload1.FileName) + "<br>");
            //文件扩展名
            Response.Write("div 内的 FileUpload1 的文件扩展名 " + System.IO.Path.GetExtension(FileUpload1.FileName) + "<br>");
            //文件全路径
            Response.Write("div 内的 FileUpload1 的文件全路径 " + FileUpload1.FileName + "<br>");
            //文件类型
            Response.Write("div 内的 FileUpload1 的文件类型 " + FileUpload1.PostedFile.ContentType + "<br>");
            //文件大小
            Response.Write("div 内的 FileUpload1 的文件大小 " + FileUpload1.PostedFile.ContentLength + "<br>");
            //是否包含文件
            Response.Write("div 内的 FileUpload1 的是否包含文件 " + FileUpload1.HasFile + "<br>");
            
            //this 表示当前 Page对象
            //文件名
            Response.Write("div 外的 File2 的文件名 " + System.IO.Path.GetFileName(this.File2.PostedFile.FileName) + "<br>");
            //文件扩展名
            Response.Write("div 外的 File2 的文件扩展名 " + System.IO.Path.GetExtension(this.File2.PostedFile.FileName) + "<br>");
            //文件全路径
            Response.Write("div 外的 File2 的文件全路径 " + this.File2.PostedFile.FileName + "<br>");
            //文件类型
            Response.Write("div 外的 File2 的文件类型 " + this.File2.PostedFile.ContentType + "<br>");
            //文件大小
            Response.Write("div 内的 File2 的文件大小 " + this.File2.PostedFile.ContentLength + "<br>");
            
        }
    }
      

  5.   

    有个地方要注意~ 正是它导致div内的button 需要点击两次才能触发 click事件.就是上传文件的大小 
    在上传文件过程中,若您上传大型文件(例如10MB),上传会报错并中断,发生这种问题的原因在于系统默认限制上传文件大小为4096KB,故只要上传超过这个限制就会产生错误并中断,那怎么办?若您想要解除这层限制,就必须调整maxRequestLength长度限制,请在Web.Config文件中加入下列设置:<configuration>    <system.web>        <httpRuntime maxRequestLength="4096" executionTimeout="120"/>    </system.web></configuration>设置说明:(1)maxRequestLength这个属性限制文件上传的大小,是以KB为单位的,默认值为4096KB,而最大上限为2097151KB,大约是2GB。(2)executionTimeout属性则是限制文件上传的时间,以秒(s)为单位,默认值为90 s,如果您考虑到所设计的Web应用系统上传时间要超过90 s可延长设定值。补充: maxRequestLength 它是针对整个页面的所有上传文件的总和
    我的web.config<httpRuntime maxRequestLength="409600" executionTimeout="120"/>
      

  6.   

    我并不认为Button放在TABLE,DIV里会出错,除非你把TABLE和DIV放在了UPDATEPANEL中.
    以下是我的代码,没有发生任何错误<%@ Page Language="VB" AutoEventWireup="true" CodeFile="Default.aspx.vb" Inherits="_Default" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title>Untitled Page</title>
    </head>
    <body>
        <form id="form1" runat="server">
            &nbsp;<asp:ScriptManager ID="ScriptManager1" runat="server" />
            <div>
                <input id="File1" type="file" runat="server" /><br />
                <div style="width: 206px; height: 37px">
                    <asp:Button ID="Button1" runat="server" Text="Button" /></div>
            </div>
        </form>
    </body>
    </html>Partial Class _Default
        Inherits System.Web.UI.Page    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim str As String
            str = System.IO.Path.GetFileName(File1.PostedFile.FileName)
            Response.Write(File1.PostedFile.FileName)
        End Sub
    End Class正如1楼所说的,UPDATEPANEL中是不可以使用Response.Write的.
    INPUT FILE 同样不可以和UPDATEPANEL一起使用,就像你所说的把BUTTON放在了UPDATEPANEL就会出错.这是因为在UPDATEPANEL中无法取得FILE1.PostedFile.FileName的值,这个值并没有被返回给服务器,这是为什么我也不清楚.这是MICROSOFT出的AJAX的问题,还有很多其它的问题.
    如果你不需要AJAX的那种不刷新效果,完全可以不使用UPDATEPANEL.如果非要使用的话,你应该先把INPUT FILE和那个BUTTON放到其他页面中,并且那个页面不用UPDATEPANEL,然后再用IFRAME把它引入到当前页面中,这样就可以了.可能有说的不对的地方,仅供参考.
      

  7.   

    楼上的代码<div>
                <input id="File1" type="file" runat="server" /><br />
                <div style="width: 206px; height: 37px">
                    <asp:Button ID="Button1" runat="server" Text="Button" /></div>
            </div>
    你改成<div>
                <input id="File1" type="file" runat="server" /><br />
     </div>
                <div style="width: 206px; height: 37px">
                    <asp:Button ID="Button1" runat="server" Text="Button" /></div>      
    这样试试
      

  8.   

    关于在updatepanel中使用fileupload有以下几种办法Simple AJAX File Upload 
    http://www.codeproject.com/KB/ajax/simpleajaxupload.aspx设置updatepanel
    http://www.cnblogs.com/gxlxzys/archive/2008/01/04/1026522.html利用iframe解决的
    http://hi.baidu.com/jovifiona/blog/item/c4f41dd14487c1d2572c8404.html自定义控件
    http://www.cnblogs.com/JeffreyZhao/archive/2007/03/26/AjaxUploadHelper_Prototype.html
      

  9.   

    http://www.mis8.cn/cmsview-1281.html