注册 登录
编程论坛 ASP.NET技术论坛

C#File控件问题

mijia 发布于 2010-05-24 18:36, 457 次点击
<%@ Page Language="C#" AutoEventWireup="true" %>
<script runat="server">
    void Button_Click(object sender, EventArgs e)
    {
        HtmlInputFile file = (HtmlInputFile)place.FindControl("Filel");
        if (Text1.Value == "")
        {
            Span1.InnerHtml = "Error:You must enter a file name.";
            return;
        }
        if (file.PostedFile.ContentLength > 0)
        {
            try
            {
                file.PostedFile.SaveAs("H:\\ASP\\" + Text1.Value);
                Span1.InnerHtml = "File uploaded successfully to" +
                    "<b>H:\\Asp\\" + Text1.Value + "</b> on the web server.";
            }
            catch (Exception exc)
            {
                Span1.InnerHtml = "Error saving flie <b>H:\\ASP\\" +
                    Text1.Value + "</b><br>" + exc.ToString() + ".";     
            }
        }
    }
    void page_Load(object sender, EventArgs e)
    {
        HtmlInputFile file = new HtmlInputFile();
        file.ID = "File";
        place.Controls.Clear();
        place.Controls.Add(file);
    }
   
</script>



<html xmlns="http://www. >
<head>
    <title>HtmlInputFile Constructor Example</title>
</head>
<body>
     <div>HtmlInputFile Constructo Exampler</div>
    <form enctype="multipart/form-data" runat="server">//“form”缺少必需的属性“action”但我不懂在那里加
    Specify the file to upload:
    <asp:placeHolder ID ="place" runat ="server"/>
    <p>
    Save as file name(no path):
    <input id ="Text1"
    type="Text"
    runat="server" />
    </p>
    <p>
    <span id ="Span1"
    style="font :8pt verdana;"
    runat="server"/>
    </p>
    <p>
    <input type ="button"
    id ="Button1"
    value="Upload"
    onserverclick="Button_Click"
    runat="server"/>
    </p>
    </form>
</body>
</html>
能运行
2 回复
#2
misswang2010-05-25 10:04
你的file1控件在哪里?
#3
mijia2010-05-25 13:33
我是用来编写的!不直接用File控件   :Input(File)上传控件
1