.Net中FileUpload控件進(jìn)行多張圖片上傳


今天在解決多張圖片上傳時(shí),發(fā)現(xiàn)之前的單張上傳版本,
竟然是使用FileUpload服務(wù)器控件做的,
心想,這個(gè)修改起來就方便多了,哈哈 ,
下面直接貼出代碼了,包含頁(yè)面代碼,和.cs文件代碼,
附帶一個(gè)Demo演示項(xiàng)目,有什么不懂的可聯(lián)系站長(zhǎng)討論


注:控件
<asp:FileUpload ID="FileLoad" runat="server" multiple="multiple"></asp:FileUpload>
multiple="multiple"  這一句是關(guān)鍵哦,不加的話,無法選擇多張圖片

FileUpload控件進(jìn)行多張圖片上傳Demo下載

.aspx頁(yè)面代碼

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="LoadImage.aspx.cs" Inherits="MultUpLoad.LoadImage" %>

<!DOCTYPE html>
<html>
<head runat="server">
    <title>FileUpload控件進(jìn)行多張圖片上傳</title>
</head>
<body>
    <form id="form1" runat="server">
    <%--注意:multiple="multiple" 這一句是關(guān)鍵哦,不加的話,只能選擇一張圖片--%>
    請(qǐng)選擇要上傳的圖片:<asp:FileUpload ID="FileLoad" runat="server" multiple="multiple"></asp:FileUpload>
    <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="FileLoad"
        ErrorMessage="請(qǐng)選擇圖片"></asp:RequiredFieldValidator><br />
    <asp:Button ID="btnUpload" runat="server" Width="80" Text="上傳" OnClick="btnUpload_Click">
    </asp:Button>
    <asp:Label ID="LbFail" runat="server"></asp:Label>
    </form>
</body>
</html>

.cs文件代碼



using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
/*
 * 本Demo只是對(duì)使用FileUpload控件進(jìn)行多張圖片上傳
 * 具體的上傳驗(yàn)證還需要大家自己實(shí)現(xiàn)哦
 * 作者:Json在線解析在線工具網(wǎng)--站長(zhǎng) www.hnxxbl.cn
 * 有什么不理解的,可以找站長(zhǎng)討論,QQ群:308250404
 */
namespace MultUpLoad
{
    public partial class LoadImage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        //獲取當(dāng)前項(xiàng)目的路徑
        private string upPath = System.AppDomain.CurrentDomain.BaseDirectory.ToString()   "\\Image\\";
        public bool Upload()
        {
            HttpFileCollection upFiles = Request.Files;
            if (upFiles.Count > 0 && upFiles.Count < 10)
            {
                int count = 0;
                for (int i = 0; i < upFiles.Count; i  )
                {
                    string datestr = DateTime.Now.Ticks.ToString()   i;
                    string extendName = string.Empty;
                    HttpPostedFile PostedFile = upFiles[i];
                    if (PostedFile.ContentLength > 0)
                    {
                        extendName = Path.GetExtension(PostedFile.FileName.ToLower()).Replace(".", "");
                        string savePath = upPath   datestr   "."   extendName;
                        //驗(yàn)證照片格式
                        PostedFile.SaveAs(savePath);
                        count  = 1;//上傳成功的圖片數(shù)量
                    }
                }
                LbFail.Text = "上傳成功"   count   "張圖片";
                return true;
            }
            else
            {
                LbFail.Text = "上傳失敗,一次最多上傳10張照片";
                return false;
            }
        }
        protected void btnUpload_Click(object sender, EventArgs e)
        {
            Upload();
        }
    }
}


原文鏈接:.Net中FileUpload控件進(jìn)行多張圖片文件上傳