FileUpLoad 多文件上传?

claymore1114 2009-11-23 09:48:54
FileUpLoad 多文件上传? 怎么实现啊。 批量上传
...全文
3698 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
pilicat 2010-05-04
  • 打赏
  • 举报
回复
谢谢各位兄弟姐妹。
woshifou 2009-12-20
  • 打赏
  • 举报
回复
mark
happy664618843 2009-11-23
  • 打赏
  • 举报
回复
document.getElementById("td_uploadFile").insertAdjacentHTML("beforeEnd",strFile);
这个方法在FireFox不管用最好用 div1.appendChild()
wuyq11 2009-11-23
  • 打赏
  • 举报
回复
<script type="text/javascript">
function AddFile()
{
var strFile=" <input name=\"upload_file\" type='file' class='inputText' style='WIDTH:350px;'/> <br/>";
document.getElementById("td_uploadFile").insertAdjacentHTML("beforeEnd",strFile);
}
</script>
<input id="BtnAddFile" type="button" class="inputButton" value="增加附件" onclick="javascript:AddFile()" />

<td id="td_uploadFile" align="center">
<input id="upload_file" name="upload_Attachment" type="file" class="inputText"
style="width: 350px; display:none;" runat="server" />
</td>
HttpFileCollection Files = HttpContext.Current.Request.Files;
for (int i = 0; i < Files.Count; i++)
{

HttpPostedFile PostedFile = Files[i];
if (PostedFile.ContentLength > 0)
{}
}
组件如neatupload、SWFUload
http://topic.csdn.net/u/20091119/16/354c197e-97c4-4f1b-9770-99a8dbdf7412.html?12676
lovexilove 2009-11-23
  • 打赏
  • 举报
回复
文件够多还是FTP吧 这个控件不适合
daichenghua 2009-11-23
  • 打赏
  • 举报
回复
Asp.net 2.0 用 FileUpload 控件实现多文件上传(用户控件)
示例代码:
1public partial class UpMultiFileControl2 : System.Web.UI.UserControl
2{
3 protected void Page_Load(object sender, EventArgs e)
4 {
5 if (!Page.IsPostBack)
6 {
7 SaveCurrentPageFileControls();
8 }
9 }
10 protected void btAddFile_Click(object sender, EventArgs e)
11 {
12 AddOneFileControl();
13 }
14
15 /**//// 〈summary〉
16 /// 添加一个上传文件控件
17 /// 〈/summary〉
18 private void AddOneFileControl()
19 {
20 ArrayList al = new ArrayList();
21 this.tbFiles.Rows.Clear();
22 GetFileControlsFromSession();
23 HtmlTableRow htr = new HtmlTableRow();
24 HtmlTableCell htc = new HtmlTableCell();
25 htc.Controls.Add(new FileUpload());
26 htr.Controls.Add(htc);
27 this.tbFiles.Rows.Add(htr);
28 SaveCurrentPageFileControls();
29 }
30
31 /**//// 〈summary〉
32 /// 读取缓存中存储的上传文件控件集
33 /// 〈/summary〉
34 private void GetFileControlsFromSession()
35 {
36 ArrayList al = new ArrayList();
37 if (Session[“FilesControls“] != null)
38 {
39 al = (System.Collections.ArrayList)Session[“FilesControls“];
40 for (int i = 0; i 〈 al.Count; i++)
41 {
42 HtmlTableRow htr1 = new HtmlTableRow();
43 HtmlTableCell htc1 = new HtmlTableCell();
44 htc1.Controls.Add((System.Web.UI.WebControls.FileUpload)al[i]);
45 htr1.Controls.Add(htc1);
46 this.tbFiles.Rows.Add(htr1);
47 }
48 }
49 }
50
51 /**//// 〈summary〉
52 /// 保存当前页面上传文件控件集到缓存中
53 /// 〈/summary〉
54 private void SaveCurrentPageFileControls()
55 {
56 ArrayList al = new ArrayList();
57 foreach (Control controlTR in this.tbFiles.Controls)
58 {
59 if (controlTR.GetType().ToString() == “System.Web.UI.HtmlControls.HtmlTableRow“)
60 {
61 HtmlTableCell htc = (HtmlTableCell)controlTR.Controls[0];
62 foreach (Control controlFileUpload in htc.Controls)
63 {
64 if (controlFileUpload.GetType().ToString() == “System.Web.UI.WebControls.FileUpload“)
65 {
66 FileUpload tempFileUpload = (FileUpload)controlFileUpload;
67 al.Add(tempFileUpload);
68 }
69 }
70 }
71 }
72 Session.Add(“FilesControls“, al);
73 }
74
75 protected void btUpFiles_Click(object sender, EventArgs e)
76 {
77 UpLoadFiles();
78 }
79
80 /**//// 〈summary〉
81 /// 上传文件操作
82 /// 〈/summary〉
83 private void UpLoadFiles()
84 {
85 string filepath = Server.MapPath(“./“)+“UploadFiles“;
86
87 HttpFileCollection uploadedFiles = Request.Files;
88 for (int i = 0; i 〈 uploadedFiles.Count; i++)
89 {
90 HttpPostedFile userPostedFile = uploadedFiles[i];
91 try
92 {
93 if (userPostedFile.ContentLength 〉 0 )
94 {
95 userPostedFile.SaveAs(filepath + “\\“ + System.IO.Path.GetFileName(userPostedFile.FileName));
96 Response.Write(“已上传文件: \““ + filepath +“\\“+ userPostedFile.FileName +“\“〈br〉〈br〉“ );
97 }
98 }
99 catch
100 {
101 Response.Write(“上传文件: \““ + userPostedFile.FileName +“\“出错!“);
102 }
103 }
104 if (Session[“FilesControls“] != null)
105 {
106 Session.Remove(“FilesControls“);
107 }
108 }
109}

(三). 改变上传文件大小和时间限制

〈httpRuntime〉
executionTimeout=“110“ //上传等待时间
maxRequestLength=“4096“ //上传文件大小,默认为4M
〈/httpRuntime〉

上传文件大小是由上面两个参数所决定的. 涉及到安全因素,不要设得太大。
jin225 2009-11-23
  • 打赏
  • 举报
回复
FileUpload 控件实现多文件上传
这个可以上传多个文件 第一次听说 关注中
claymore1114 2009-11-23
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 lzsh0622 的回复:]
引用 5 楼 claymore1114 的回复:
添加的 一个控件时 , 原来的 选择 有文件的,消失了 怎么解决?

用 Session 保存
[/Quote]
用Session 保存了 fileUpLoad的值,不能赋值, FileName 为只读, 怎么弄?
lzsh0622 2009-11-23
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 claymore1114 的回复:]
添加的 一个控件时 , 原来的 选择 有文件的,消失了 怎么解决?
[/Quote]
用 Session 保存
claymore1114 2009-11-23
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 lzsh0622 的回复:]
FileUpload 控件实现多文件上传
[/Quote]
添加的 一个控件时 , 原来的 选择 有文件的,消失了 怎么解决?
zzxap 2009-11-23
  • 打赏
  • 举报
回复
百度一大堆
lzsh0622 2009-11-23
  • 打赏
  • 举报
回复
pdsnet 2009-11-23
  • 打赏
  • 举报
回复

function AddFile()
{
var aaa = document.getElementsByName("File1")

if(aaa.length<=2)
{
var strFile=' <input name="File1" type="file" style="width:319px" /> ';
document.getElementById("DivFile").insertAdjacentHTML("beforeEnd",strFile);
}
else
{
alert('只能上传3张生活照');
return false;
}


}
<input id="BtnAddFile" type="button" value="增加图片" onclick="javascript:AddFile()" style="width: 70px" />图片小于1M
<div id="DivFile"></div>




HttpFileCollection Files = HttpContext.Current.Request.Files;

//生活照
if (Files.Count > 0)
{


for (int i = 1; i < Files.Count; i++)
{


if (Files[i].FileName.Length > 5)
{
arraypic.Add(upimg(Files[i], "~/VideoImg", this.Page));


}

}



}
mohugomohu 2009-11-23
  • 打赏
  • 举报
回复
这个啊,我是用ftp的方式的

分享:批量上传图片
淮少 2009-11-23
  • 打赏
  • 举报
回复
4兆
telankes2000 2009-11-23
  • 打赏
  • 举报
回复


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

<!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>
<style type="text/css">
.myspan { BACKGROUND-COLOR: blue; HEIGHT: 20px; COLOR: #fff; CURSOR: pointer; boder: solid 1px ccc }
</style>

<script language="javascript">
var tempID=1;
function AddFile(obj)
{
if(tempID>4)
{
alert("最多只能同時上傳5個文件")
return;
}
var filenfo = document.getElementById("FilesInfo");
var odiv = document.createElement("div");
odiv.id = "div_"+tempID;
odiv.innerHTML +="<input type ='file' name ='File' id='File_"+tempID+"' size='55'><span title='取消' onclick='removeFile("+tempID+")' class='myspan'>取消</span>";
filenfo.appendChild(odiv);
tempID++;
}
function removeFile(removeID)
{

var FileInfo = document.getElementById("FilesInfo");
var tempFiles = document.getElementById("div_"+removeID);
if(tempFiles)
{
FileInfo.removeChild(tempFiles)
tempID = tempID-1;
}
}
function StartUpFile()
{
if(document.getElementById("File0").value == "")
{
document.getElementById("lblMessage").innerHTML= "請選擇上傳文件";
event.returnValue = false;
return;
}
document.getElementById("lblMessage").innerHTML = " 正在上傳,請稍候...";
}


</script>

</head>
<body>
<form id="form1" runat="server" enctype="multipart/form-data">
<div id="Add">
<b>上傳文件</b>
<div id="baseFile">
<input id="File0" type="file" name="Files0" size="55"></div>
<div id="FilesInfo">
</div>
<div>
<asp:Button ID="btnUpFile" runat="server" Text="開始上傳" OnClientClick="StartUpFile()" OnClick="btnUpFile_Click">
</asp:Button>
<input onclick="AddFile()" type="button" value="添加文件" style="width: 79px; height: 24px">
</div>
<asp:Label ID="lblMessage" runat="server" ForeColor="#ff0066"></asp:Label></div>
</form>
</body>
</html>
[code=C#]
protected void btnUpFile_Click(object sender, EventArgs e)
{
HttpFileCollection files = Request.Files;
HttpPostedFile postFile;
for (int i = 0; i < files.Count; i++)
{
postFile = files[i];
if (postFile.ContentLength == 0) continue;
UpLoadFile(postFile, Server.MapPath("~/") + Path.GetFileName(postFile.FileName));
}
Response.Write("上傳成功");

}
public void UpLoadFile(System.Web.HttpPostedFile postFile, string savePath)
{
try
{
if (!Directory.Exists(Path.GetDirectoryName(savePath)))
Directory.CreateDirectory(Path.GetDirectoryName(savePath));
using (FileStream fs = new FileStream(savePath, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite))
{
using (BinaryReader reader = new BinaryReader(postFile.InputStream))
{
int bufferLength = 0;
int readLength = 10240;
byte[] buffer = new byte[10240];
while (true)
{
bufferLength = reader.Read(buffer, 0, readLength);
if (bufferLength == 0) break;
fs.Write(buffer, 0, bufferLength);
}
}
}
}
catch (Exception ex)
{
throw new Exception(ex.Source + ex.Message);
}
}

[/code]

62,046

社区成员

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

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

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

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