62,046
社区成员
发帖
与我相关
我的任务
分享
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="上传多个文件11.WebForm1" %>
<!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 id="Head1" runat="server">
<title>同时上载多个文件</title>
<script language="javascript" type="text/javascript">
function addOneFile() {
var file = document.getElementsByName("file");
if (file.length < 20) {
var filebutton = '<br /><input type="file" size="50" name="file" />';
document.getElementById('pFileList').insertAdjacentHTML("beforeEnd", filebutton);
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<p id="pFileList"><input type="file" size="50" name="file" /></p>
<input type="button" value='增加一个文件' onclick="addOneFile()" />
<asp:Button ID="ButtonUpload" runat="server" Text="上传"
onclick="ButtonUpload_Click" />
</form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace 上传多个文件11
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void ButtonUpload_Click(object sender, EventArgs e)
{
HttpFileCollection files = HttpContext.Current.Request.Files;
if (files == null)
{
return;
}
try
{
for (int i = 0; i < files.Count; i++)
{
HttpPostedFile postFile = files[i];
if (postFile == null)
{
continue;
}
string fileName = postFile.FileName;
string folderPath =
Server.MapPath("Attachments" + "\\" + DateTime.Now.ToString("yyyy-MM"));
string fullPath = folderPath + fileName ;
if (System.IO.Directory.Exists(folderPath) == false)
{
System.IO.Directory.CreateDirectory(folderPath);
}
postFile.SaveAs(fullPath);
}
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
}
}
}