关闭

ASP.NET 2.0中上传文件的简单实现

650人阅读 评论(0) 收藏 举报

 

//file name: Default.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

 

<!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>

    <h3>HtmlInputFile Example</h3>

 

    <form id="Form1" enctype="multipart/form-data" runat="server">

 

       Select File to Upload:

       <input id="File1" type="file" runat="server"/>

       <p>

       <span id="Span1" style="font: 8pt verdana;" runat="server" />

        </p>

       <p>

       <input type="button" id="Button1" value="Upload" onserverclick="Button1_Click" runat="server" />

       </p>

    </form>

</body>

</html>

 

//file name: Default.cs

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.IO;

 

public partial class _Default : System.Web.UI.Page

{

    protected void Page_Load(object sender, EventArgs e)

    {

    }

    protected void Button1_Click(object sender, EventArgs e)

    {

        // Save the file.

        if (File1.PostedFile.ContentLength > 0)

        {

            string path = "";

            path = Page.MapPath(".");

            try

            {

                File1.PostedFile.SaveAs(path + "//msxml.exe");

                Span1.InnerHtml = "File uploaded successfully to <b>" + path + "//msxml.exe" + "</b> on the Web server.";

            }

            catch (Exception exc)

            {

                Span1.InnerHtml = "Error saving file <b>" + path + "//msxml.exe" + "</b><br>" + exc.ToString() + ".";

            }

        }

    }

}

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:266511次
    • 积分:3523
    • 等级:
    • 排名:第9371名
    • 原创:69篇
    • 转载:125篇
    • 译文:0篇
    • 评论:37条
    最新评论
    项目管理
    政策、法律法规