同时上传多个文件

u010046044 2013-05-07 07:42:16
现在正在用asp.net做一个 同时上传多个文件 的程序

下面是HTML代码:

<%@ 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>



下面是c#代码:

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);
}
}






}
}



为什么
HttpFileCollection files = HttpContext.Current.Request.Files; 会读不出文件?
...全文
170 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
a102470 2013-05-08
  • 打赏
  • 举报
回复
<form id="form1" runat="server" enctype = "multipart/form-data">
geass 2013-05-08
  • 打赏
  • 举报
回复
引用 2 楼 u010046044 的回复:
还有没有别的解决办法啊?
你的form 缺少 enctype = "multipart/form-data" 属性
  • 打赏
  • 举报
回复
引用 2 楼 u010046044 的回复:
还有没有别的解决办法啊?
什么意思?
u010046044 2013-05-07
  • 打赏
  • 举报
回复
还有没有别的解决办法啊?

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧