ASP.NET 2.0 多檔案上傳小經驗

來源:互聯網
上載者:User

   想實現任意多個檔案上傳的功能,點擊一次按鈕可以添加一個檔案上傳框,以前在網路硬碟上看到過。JavaScript我知道怎麼實現任意添加上傳檔案控制項,問題是添加的是Html控制項,我不懂怎麼讓伺服器端可以擷取檔案。

    於是上google搜尋“ASP.NET 多檔案上傳”,還真找到一篇檔案,標題為《在ASP.NET中實現多檔案上傳》,文章裡面是VB.NET實現的,功能和我要的一模一樣,我主要是要看伺服器端怎麼擷取用戶端上傳的檔案,看了文中的代碼,原來這麼簡單,System.Web.HttpContext.Current.Request.Files就包含用戶端瀏覽器上傳的檔案了,我用C#寫了一段簡單的代碼,原本以為應該可以了,結果出乎意料上傳3個圖片System.Web.HttpContext.Current.Request.Files返回的檔案格式還是0個。

    不知道什麼原因,看看代碼,這麼簡單不可能些錯啊,再看看google搜尋結果裡的另外幾篇文章,發現我看的第一篇不是原做,作者的網站上原作的執行個體有兩個版本,一個是VB.NET一個是C#的,現在我不用自己寫了,複製原文的代碼到本地,運行,果然可以啊,那我寫的代碼怎麼不行?反覆比對My Code和文章中代碼的區別,試了幾個地方,最後發現和其他地方都沒有關係,原因出在<form id="form1" runat="server" enctype="multipart/form-data"> 的enctype屬性上,VS 2005建的頁面裡沒有這個屬性,而文章執行個體裡有,我後來加上enctype="multipart/form-data"後System.Web.HttpContext.Current.Request.Files就能z正常擷取檔案個數了。

    大概是VS 2003建的頁面預設有這個屬性吧,否則這麼重要的屬性作者應該會在文章中提到的。

參考:
《在ASP.NET中實現多檔案上傳》

我做的實驗代碼:

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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script language="JavaScript">
function addFile()
{
 var str = '<INPUT type="file" size="50" NAME="File">'
 document.getElementById('MyFile').insertAdjacentHTML("beforeEnd",str)
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server" enctype="multipart/form-data">
           <input type="button" value="增加(Add)" onclick="addFile()">
          <input onclick="this.form.reset()" type="button" value="重設(ReSet)">
          <asp:Button Runat="server" Text="上傳" ID="Upload" OnClick="Upload_Click1" ></asp:Button>
    <div id="MyFile">
         <input type="file" name="File" />
    </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;

namespace Demo
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }
        protected void Upload_Click1(object sender, EventArgs e)
        {

            HttpFileCollection _files = System.Web.HttpContext.Current.Request.Files;

            for (int i = 0; i < _files.Count; i++)
            {
                _files[i].SaveAs(Server.MapPath("~/Files/" + _files[i].FileName));
            }
        }
    }
}

資料引用:http://www.knowsky.com/339704.html

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.