ASP.NET上传文件实例代码

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="FileUpload.aspx.cs" Inherits="EM_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>
    <base target=_self />
</head>
<body>
    <form id="form1" runat="server">
    <div style="margin:10px;">
        <fieldset style="300px;"><legend style="font-size:small;">上傳文件</legend>
            <table style=" 300px;">
                <tr>
                    <td>
                        <asp:Label ID="Label1" runat="server" Text="上傳文件請控制在10M以內!" Font-Size="Small" ForeColor="Red"></asp:Label></td>
                </tr>
                <tr>
                    <td style=" 300px">
                        <asp:FileUpload ID="fuSelect" runat="server" Width="296px" /></td>
                </tr>
                <tr>
                    <td style=" 300px">
                        &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;
                        <asp:Button ID="btnYes" runat="server" Text=" 上 傳 "  ToolTip="確定選用此文件並退出!"
                            onclick="btnYes_Click"/>
                        &nbsp; &nbsp;&nbsp;
                        <asp:Button ID="btnNo" runat="server" Text=" 取 消 "  ToolTip="放棄選用並退出"
                            onclick="btnNo_Click" />
                        </td>
                </tr>
            </table>
       
        </fieldset>
    </div>
    </form>
</body>
</html>

//*********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;

public partial class EM_FileUpload : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Expires = 0;
        Response.Cache.SetNoStore();
        Response.AppendHeader("Pragma", "no-cache");

    }
    protected void btnYes_Click(object sender, EventArgs e)
    {
        if (fuSelect.HasFile)
        {
            try
            {
                string FilePath = System.Web.HttpContext.Current.Request.MapPath("Datum") + "\\" + Request.QueryString["shipid"].ToString() ;
                if (!System.IO.Directory.Exists(FilePath))
                    System.IO.Directory.CreateDirectory(FilePath);

                fuSelect.SaveAs(FilePath + "\\" + fuSelect.FileName);
                //Label2.Text = "客戶端路徑:" + fuSelect.PostedFile.FileName + "<br>" +
                //              "文件名:" + System.IO.Path.GetFileName(fuSelect.FileName) + "<br>" +
                //              "文件大小:" + fuSelect.PostedFile.ContentLength + " KB<br>";
                //Label1.Text = System.IO.Path.GetFileName(fuSelect.FileName).ToString().Trim();
                //Session["Shipment"] = System.IO.Path.GetFileName(fuSelect.FileName).ToString().Trim();
                //ibtnYes.Enabled = true;
            }
            catch (Exception ex)
            {
                Label1.Text = "發生錯誤:" + ex.Message.ToString();
                return;
                //ibtnYes.Enabled = false;
            }
        }
        else
        {
            Label1.Text = "請選擇要上傳的文件!";
            return;

        }

        //Page.RegisterStartupScript("MobileMSG", "<script>window.dialogArguments.document.getElementById('ctl00_ContentPlaceHolder1_lblEquPhoto').innerHTML ='" + Label1.Text.ToString() + "';</script>");
        Page.RegisterStartupScript("CancelSelect", "<script>self.close();</script>");
    }
    protected void btnNo_Click(object sender, EventArgs e)
    {
        Page.RegisterStartupScript("CancelSelect", "<script>self.close();</script>");
    }
}

原文地址:https://www.cnblogs.com/vic_lu/p/1810108.html