ASP.NET(C#)實現一次性上傳多張圖片(多個檔案)

來源:互聯網
上載者:User

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

<!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>ASP.NET(C#)實現一次性上傳多張圖片(多個檔案)</title>
   
<script type="text/javascript">
var i=1
function addFile()
{

 if (i<8)
    {var str = '<BR> <input type="file" name="File" runat="server" style="width: 200px"/>描述:<input name="text" type="text" style="width: 150px" maxlength="20" />'
    document.getElementById('MyFile').insertAdjacentHTML("beforeEnd",str)

    }
else
    {
        alert("您一次最多隻能上傳8張圖片!")
    }
    i++
}
</script>
       
       
</head>
<body>
    <form id="form1" runat="server">
   <div>
        <table id="Table1" align="center" border="0" cellpadding="1" cellspacing="1" class="table">

            <tr>
                <td align="center">
                    <font color="#0000ff" face="宋體" size="3"><strong>上傳圖片</strong></font></td>
            </tr>
            <tr>
                <td align="center" style="width: 734px">
                    &nbsp;</td>
            </tr>
            <tr>
                <td align="center" style="width: 734px">
                    <asp:Panel ID="Panel5" runat="server">
                        &nbsp; &nbsp;<table width="100%">
                            <tr>
                                <td align="right" style="width: 100px">
                                </td>
                                <td align="left" style="width: 500px">
                                    說明:點增加圖片按鈕可一次上傳多張圖片,可為每張圖片寫上一句不超過20個字的描述。單張圖片大小不大於1024k</td>
                            </tr>
                            <tr>
                                <td align="right" style="width: 100px">
                                    請選擇圖片:<br />
                                </td>
                                <td align="left" style="width: 500px"><p id="MyFile"><input onclick="addFile()" type="button" value="增加圖片(Add)"></p><br />
                                    <input id="File1" type="file" name="File" runat="server" style="width: 245px"/>
                                    描述:<input name="text" type="text" style="width: 150px" maxlength="20" />
                                </td>
                            </tr>

                            <tr>
                                <td align="right" style="width: 100px">
                                </td>
                                <td align="left" style="width: 500px">
                                    <asp:Button ID="btnUpload" runat="server" Text="開始上傳" OnClick="btnUpload_Click"  />
                                    </td>
                            </tr>
                            <tr>
                                <td align="right" style="width: 100px">
                                </td>
                                <td align="left" style="width: 500px">
                                    <asp:Label ID="lblMessage" runat="server" Font-Bold="True" ForeColor="Red"></asp:Label></td>
                            </tr>
                        </table>
                    </asp:Panel>
                    &nbsp;
                </td>
            </tr>
            <tr>
                <td align="center" style="width: 734px">
                    <font face="宋體"></font><font face="宋體">&nbsp;</font>
                </td>
            </tr>
        </table>
   
    </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;

using System.IO;

public partial class FileUp : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnUpload_Click(object sender, EventArgs e)
    {
        lblMessage.Text = "";
        lblMessage.Visible = false;
        System.Web.HttpFileCollection files = System.Web.HttpContext.Current.Request.Files;
        System.Text.StringBuilder strmsg = new System.Text.StringBuilder("");
        string[] rd = Request.Form[1].Split(',');//獲得圖片描述的文字框字串數組,為對應的圖片的描述
        //string albumid=ddlAlbum.SelectedValue.Trim();
        int ifile;
        for (ifile = 0; ifile < files.Count; ifile++)
        {
            if (files[ifile].FileName.Length > 0)
            {
                System.Web.HttpPostedFile postedfile = files[ifile];
                if (postedfile.ContentLength / 1024 > 1024)//單個檔案不能大於1024k
                {
                    strmsg.Append(Path.GetFileName(postedfile.FileName) + "---不能大於1024k<br>");
                    break;
                }
                string fex = Path.GetExtension(postedfile.FileName);
                if (fex != ".jpg" && fex != ".JPG" && fex != ".gif" && fex != ".GIF")
                {
                    strmsg.Append(Path.GetFileName(postedfile.FileName) + "---圖片格式不對,只能是jpg或gif<br>");
                    break;
                }
            }
        }
        if (strmsg.Length <= 0)//說明圖片大小和格式都沒問題
        {
            //以下為建立圖庫目錄
            string dirpath = Server.MapPath("51aspx");

            if (Directory.Exists(dirpath) == false)
            {
                Directory.CreateDirectory(dirpath);
            }
            Random ro = new Random();
            int name = 1;
            for (int i = 0; i < files.Count; i++)
            {
                System.Web.HttpPostedFile myFile = files[i];
                string FileName = "";
                string FileExtention = "";
                FileName = System.IO.Path.GetFileName(myFile.FileName);
                string stro = ro.Next(100, 100000000).ToString() + name.ToString();//產生一個隨機數用於新命名的圖片
                string NewName = DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString() + stro;
                if (FileName.Length > 0)//有檔案才執行上傳操作再儲存到資料庫
                {
                    FileExtention = System.IO.Path.GetExtension(myFile.FileName);

                    string ppath = dirpath + @"\" + NewName + FileExtention;
                    myFile.SaveAs(ppath);

                }

                name = name + 1;//用來重新命名規則的變數

            }

            Response.Write("<script>alert('恭喜,圖片上傳成功!')</script>");
        }
        else
        {
            lblMessage.Text = strmsg.ToString();
            lblMessage.Visible = true;
        }
    }
}

相關文章

聯繫我們

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