C# asp.net實現檔案上傳

來源:互聯網
上載者:User

標籤:function   asp.net   visual   開發   null   

前端代碼:使用visual studio開發實現檔案上傳前端頁面代碼:<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="scientist.WebForm1" %><!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"><script type="text/javascript">        var baseText = null;     function upOpen() {        var xzOpen = document.getElementById("xzOpen");        xzOpen.style.top = "200px"; //視窗距離瀏覽器內容區最上方的位移值         xzOpen.style.left = "500px"; //視窗距離瀏覽器內容區最左邊的位移值         xzOpen.style.width = "500px"; //視窗的寬度         xzOpen.style.height = "300px"; //視窗的高度        if (baseText == null) baseText = xzOpen.innerHTML;        xzOpen.innerHTML = baseText + "<div id=\"statusbar\"><button onclick=\"hidePopup(); \">Close window<button></div>"        var sbar = document.getElementById("statusbar");         sbar.style.marginTop = (parseInt(100)-20) + "px";        xzOpen.style.visibility = "visible";     document.getElementById("xzOpen").click();    }</script> <title>上傳文檔</title><style type="text/css"> *{ margin:0; padding:0;} .exDiv{}.boxmain{float:left;margin-right:0px;width:100%;} .xzOpen{position: absolute; visibility: hidden; overflow: hidden; border:2px solid #CCC; background-color:#FFCBB3; border:2px solid #333; padding:5px; }.F1{float:left;margin-top:5px;}.B1{float:right;margin-top:80px;}.left{position:absolute;left:0; background:#BBFFBB;height:300px;width:20%} .main{margin-right:200px;background:#79FF79; height:300px;;width:100%;margin-left:auto;} .up{margin-right:1px;background:#984B4B; height:30px;width:64px;margin-left:0px;    }</style> </head><body>    <form id="form1" runat="server">    <div class="exDiv" style="width:100%; height:80px; margin:0 auto; border:solid 1px #999999;background-color:#95CACA">    <font >上傳檔案</font>    </div>    <div class="xzOpen" id="xzOpen" >        <div class="F1" id="F1">            <asp:FileUpload ID="FileUpload1" runat="server" Width="224px"  />         </div>         <div class="B1" id="B1">            <asp:Button ID="Button1" runat="server" Text="提交" onclick="Button1_Click"  style="margin:0 auto" Width="107px" />         </div>    </div>    </form>    <div class="boxmain">         <div class="main">main</div>     </div>     <div class="left"><input type="file" id="xzFile" style="display:none"/>          <button type="button" class="up" onclick="upOpen()">選擇檔案</button>    </div> </body></html>後台C#部分:using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;namespace scientist{    public partial class WebForm1 : System.Web.UI.Page    {        protected void Page_Load(object sender, EventArgs e)        {        }        protected void Button1_Click(object sender, EventArgs e)        {            HttpPostedFile postedFile = this.FileUpload1.PostedFile; //擷取到要上傳的檔案            String fileName = "";//檔案名稱            String filePath = "filesNameTest/";//檔案儲存路徑            fileName = System.IO.Path.GetFileName(postedFile.FileName);//擷取檔案名稱            if (System.IO.Directory.Exists(Server.MapPath(filePath)) == false)//判斷檔案夾是否存在            {                System.IO.Directory.CreateDirectory(Server.MapPath(filePath));//如果不存在就建立file檔案夾            }            if (System.IO.File.Exists(Server.MapPath(filePath+fileName)) == true)//判斷同名檔案是否存在            {                Page.ClientScript.RegisterStartupScript(this.GetType(), "message", "alert(‘同名檔案已存在‘)", true);//彈窗提示檔案已存在            }            else//檔案不存在則儲存檔案            {                if (fileName != "")//判斷前端是否有檔案傳過來                {                    String fileSuffix = System.IO.Path.GetExtension(fileName); //擷取上傳檔案的副檔名                    postedFile.SaveAs(System.Web.HttpContext.Current.Request.MapPath(filePath) + fileName);//儲存檔案至根目錄下的files檔案夾裡                    Page.ClientScript.RegisterStartupScript(this.GetType(), "message", "alert(‘已經儲存成功‘)", true);//彈窗提示儲存成功                }                else                {                    Page.ClientScript.RegisterStartupScript(this.GetType(), "message", "alert(‘請選擇檔案‘)", true);//彈窗提示未選擇檔案                }            }        }    }}

本文出自 “技術的秘密-劉聰聰的創作” 部落格,請務必保留此出處http://liucongcong.blog.51cto.com/11527480/1929800

C# asp.net實現檔案上傳

相關文章
阿里云产品大规模降价
  • 最高幅度達59%,平均降幅23%
  • 核心產品降價
  • 多地區降價
undefined. /
透過 Discord 與我們聯繫
  • 安全、匿名的群聊,不受干擾
  • 隨時了解活動、活動、新產品等訊息
  • 支持您的所有問題
undefined. /
免費試用
  • 開啟從ECS到大數據的免費試用之旅
  • 只需三步 輕鬆上雲
  • 免費試用ECS t5 1C1G
undefined. /

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.