asp.net html控制項的File控制項實現多檔案上傳執行個體分享

來源:互聯網
上載者:User

asp.net多檔案上傳使用html控制項的File控制項,在form中就需要加入【 enctype="multipart/form-data"】。
up3.aspx檔案代碼 複製代碼 代碼如下:<%@ Page Language="C#" AutoEventWireup="true" CodeFile="up3.aspx.cs" Inherits="up3" %>
<!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>
<script language="javascript" type="text/javascript">
function addFile() {
var odiv = document.getElementById("MyFile");
var str = "<div><input name='File' type='file' /></div>";
odiv.insertAdjacentHTML("beforeEnd", str);
}
function resetFile() {
var odiv = document.getElementById("MyFile");
odiv.innerHTML = "<div><input name='File' type='file' /></div>";
}
</script>
</head>
<body>
<form id="form1" runat="server" enctype="multipart/form-data">
<input type="button" value="增加" onclick="addFile()" />
<input type="button" value="重設" onclick="resetFile()" />
<div id="MyFile">
<div><input name="File" type="file" /></div>
</div>
<asp:Button runat="server" Text="上傳" ID="Button1" OnClick="Button1_Click" BorderColor="Desktop"
BorderWidth="1px" Height="20px" Width="60px"></asp:Button>
<div>
<asp:Label ID="Label1" runat="server"></asp:Label>
</div>
</form>
</body>
</html>

up3.aspx.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;
public partial class up3 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
string upPath = "/up/"; //上傳檔案路徑
int upLength = 5; //上傳檔案大小
string upFileExtName = "|bmp|jpg|jpeg|png|gif|";
HttpFileCollection _files = System.Web.HttpContext.Current.Request.Files;
int flag = _files.Count;
int flagN = 0;
int flagE = 0;
int flagEE = 0;
string flagEEstr = "";
for (int i = 0; i < _files.Count; i++)
{
string name = _files[i].FileName;
FileInfo fi = new FileInfo(name);
string oldfilename = fi.Name;
string scExtension = fi.Extension.ToLower();
string fileName = DateTime.Now.ToString("yyyyMMddhhmmssfff") + fi.Extension; // 檔案名稱,目前時間(yyyyMMddhhmmssfff)
string webFilePath = Server.MapPath(upPath) + fileName; // 伺服器端檔案路徑
if (upFileExtName.IndexOf(scExtension.Replace(".", "")) == -1)
{
flagEE = flagEE + 1;
flagEEstr = flagEEstr + "第" + (i + 1) + "個檔案,檔案名稱[" + oldfilename + "],檔案類型不符合!";
continue;
}
if ((fi.Length / (1024 * 1024)) > upLength)
{
flagEE = flagEE + 1;
flagEEstr = flagEEstr + "第" + (i + 1) + "個檔案,檔案名稱[" + oldfilename + "],超出" + upLength + "M大小限制!";
continue;
}
try
{
_files[i].SaveAs(webFilePath);
}
catch (Exception ex)
{
flagEE = flagEE + 1;
flagEEstr = flagEEstr + "第" + (i + 1) + "個檔案,上傳異常【"+ex.Message+"】";
}
}
Label1.Text = "總檔案【" + flag + "】,上傳成功檔案【" + flagN + "】,異常檔案【" + (flagE + flagEE) + "】【" + flagEEstr + "】";
}
}

相關文章

聯繫我們

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