国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

ASP.NET多文件上傳實(shí)例

2019-11-17 03:16:07
字體:
供稿:網(wǎng)友

asp.net多文件上傳實(shí)例

在Web應(yīng)用程序開發(fā)中,避免不了要用到上傳文件這個(gè)功能,但以前上傳文件是個(gè)很麻煩的事,現(xiàn)在有了.NET,文件上傳變得輕而易舉。下面的這個(gè)例子實(shí)現(xiàn)了多文件上傳功能。可以動(dòng)態(tài)添加輸入表單,上傳的文件數(shù)量沒有限制。代碼如下:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="MultiFileUpload.aspx.cs"

Inherits="MultiFileUpload" %>

<!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多文件上傳測試</title>

<script type="text/javascript">

function addFile() {

var div = document.createElement("div");

var f = document.createElement("input");

f.setAttribute("type", "file")

f.setAttribute("name", "File")

f.setAttribute("size", "50")

div.appendChild(f)

var d = document.createElement("input");

d.setAttribute("type", "button")

d.setAttribute("onclick", "deteFile(this)");

d.setAttribute("value", "移除")

div.appendChild(d)

document.getElementById("_container").appendChild(div);

}

function deteFile(o) {

while (o.tagName != "DIV") o = o.parentNode;

o.parentNode.removeChild(o);

}

</script>

</head>

<body>

<form id="form1" runat="server" method="post" enctype="multipart/form-data">

<h3>多文件上傳</h3>

用戶名:<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>

<div id="_container">

<input type="file" size="50" name="File" />

</div>

<div>

<input type="button" value="添加文件(Add)" onclick="addFile()" />

</div>

<div style="padding:10px 0">

<asp:Button runat="server" Text="開始上傳" ID="UploadButton"

onclick="UploadButton_Click"></asp:Button>

</div>

<div>

<asp:Label ID="strStatus" runat="server" Font-Names="宋體" Font-Bold="True" Font-Size="9pt"

Width="500px" BorderStyle="None" BorderColor="White"></asp:Label>

</div>

</form>

</body>

</html>

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

public partial class MultiFileUpload : System.Web.UI.Page

{

PRotected void UploadButton_Click(object sender, EventArgs e)

{

///'遍歷File表單元素

HttpFileCollection files = HttpContext.Current.Request.Files;

/// '狀態(tài)信息

System.Text.StringBuilder strMsg = new System.Text.StringBuilder("您輸入的用戶名是:" + TextBox1.Text + "<br/>");

strMsg.Append("上傳的文件分別是:<hr color='red'/>");

try

{

for (int iFile = 0; iFile < files.Count; iFile++)

{

///'檢查文件擴(kuò)展名字

HttpPostedFile postedFile = files[iFile];

string fileName, fileExtension;

fileName = System.IO.Path.GetFileName(postedFile.FileName);

if (fileName != "")

{

fileExtension = System.IO.Path.GetExtension(fileName);

strMsg.Append("上傳的文件類型:" + postedFile.ContentType.ToString() + "<br>");

strMsg.Append("客戶端文件地址:" + postedFile.FileName + "<br>");

strMsg.Append("上傳文件的文件名:" + fileName + "<br>");

strMsg.Append("上傳文件的擴(kuò)展名:" + fileExtension + "<br><hr>");

///'可根據(jù)擴(kuò)展名字的不同保存到不同的文件夾

///注意:可能要修改你的文件夾的匿名寫入權(quán)限。

postedFile.SaveAs(System.Web.HttpContext.Current.Request.MapPath("images/") + fileName);

}

}

strStatus.Text = strMsg.ToString();

}

catch (System.Exception Ex)

{

strStatus.Text = Ex.Message;

}

}

}


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 阿尔山市| 封开县| 武平县| 松江区| 三河市| 宁德市| 赞皇县| 宝鸡市| 通河县| 吴旗县| 闽清县| 门头沟区| 雷州市| 常宁市| 五华县| 呈贡县| 兴城市| 呼图壁县| 连云港市| 道真| 毕节市| 萍乡市| 南乐县| 和硕县| 蛟河市| 东平县| 连州市| 明溪县| 古交市| 陕西省| 安新县| 章丘市| 驻马店市| 修武县| 抚顺县| 六盘水市| 腾冲县| 旌德县| 繁昌县| 通榆县| 沛县|