想做一个多文件上传功能,具体实现是这样的:
1、点击“浏览”可添加文件到集合,添加一个就在后台动态添加一个TextBox跟FileUpload ,添加的文本用来保存路径。
问题: 现在添加部分没问题了,也能或如Request.Files的集合,现在问题是集合里面的上传路径不存在,因为路径只
保留在TextBox中,如何才能获取路径保存在FileUpload中实现上传? 希望大家一同参与讨论,谢谢了
前台代码: 我用的是Repeater绑定控件
<asp:Panel ID="Panel1" runat="server" CssClass="pan">
<asp:Repeater ID="ReptAddFile" runat="server"
onitemcommand="ReptAddFile_ItemCommand">
<ItemTemplate>
<div id="divname<%#Eval("id") %>" style="width:85%; float:left;" onmouseover="changeColor(<%#Eval("id") %>);" onmouseout="cleanColor(<%#Eval("id") %>);" >
<asp:Label ID="lblFilePath" runat="server" CssClass="text" Text='<%#Eval("filepath") %>' ></asp:Label>
</div>
<div id="divfile<%#Eval("id") %>" style="width:10%; float:left; text-align:right;" onmouseover="changeColor(<%#Eval("id") %>);" onmouseout="cleanColor(<%#Eval("id") %>);" >
<asp:FileUpload ID="FileUploadName" CssClass="file" runat="server" />
<asp:LinkButton ID="bntDelete" CommandName="delete" CssClass="button" runat="server">删除</asp:LinkButton>
</div>
</ItemTemplate>
</asp:Repeater>
</asp:Panel>
后台代码:
/// <summary>
/// 绑定上传文件
/// </summary>
protected void FileUploadAdd()
{
DataTable table = new DataTable("files");
DataColumn c1 = new DataColumn("filepath", typeof(String));
DataColumn c2 = new DataColumn("id", typeof(Int32));
table.Columns.Add(c2);
table.Columns.Add(c1);
for (int i = 1; i <= count; i++)
{
table.Rows.Add(table.NewRow());
table.Rows[i - 1]["filepath"] = arr[i - 1].ToString();
table.Rows[i - 1]["id"] = i.ToString();
}
ReptAddFile.DataSource = table;
ReptAddFile.DataBind();
} 注释:count是个静态变量,用来控制当前添加上传文件的数量
arr 是ArrayList数组用来保存当前上传文件路径的集合
1、点击“浏览”可添加文件到集合,添加一个就在后台动态添加一个TextBox跟FileUpload ,添加的文本用来保存路径。
问题: 现在添加部分没问题了,也能或如Request.Files的集合,现在问题是集合里面的上传路径不存在,因为路径只
保留在TextBox中,如何才能获取路径保存在FileUpload中实现上传? 希望大家一同参与讨论,谢谢了
前台代码: 我用的是Repeater绑定控件
<asp:Panel ID="Panel1" runat="server" CssClass="pan">
<asp:Repeater ID="ReptAddFile" runat="server"
onitemcommand="ReptAddFile_ItemCommand">
<ItemTemplate>
<div id="divname<%#Eval("id") %>" style="width:85%; float:left;" onmouseover="changeColor(<%#Eval("id") %>);" onmouseout="cleanColor(<%#Eval("id") %>);" >
<asp:Label ID="lblFilePath" runat="server" CssClass="text" Text='<%#Eval("filepath") %>' ></asp:Label>
</div>
<div id="divfile<%#Eval("id") %>" style="width:10%; float:left; text-align:right;" onmouseover="changeColor(<%#Eval("id") %>);" onmouseout="cleanColor(<%#Eval("id") %>);" >
<asp:FileUpload ID="FileUploadName" CssClass="file" runat="server" />
<asp:LinkButton ID="bntDelete" CommandName="delete" CssClass="button" runat="server">删除</asp:LinkButton>
</div>
</ItemTemplate>
</asp:Repeater>
</asp:Panel>
后台代码:
/// <summary>
/// 绑定上传文件
/// </summary>
protected void FileUploadAdd()
{
DataTable table = new DataTable("files");
DataColumn c1 = new DataColumn("filepath", typeof(String));
DataColumn c2 = new DataColumn("id", typeof(Int32));
table.Columns.Add(c2);
table.Columns.Add(c1);
for (int i = 1; i <= count; i++)
{
table.Rows.Add(table.NewRow());
table.Rows[i - 1]["filepath"] = arr[i - 1].ToString();
table.Rows[i - 1]["id"] = i.ToString();
}
ReptAddFile.DataSource = table;
ReptAddFile.DataBind();
} 注释:count是个静态变量,用来控制当前添加上传文件的数量
arr 是ArrayList数组用来保存当前上传文件路径的集合
解决方案 »
- json2_min.js,MyUtils.js项目里使用这个是什么?
- 最近在做一个asp.net的论坛 关于回帖问题上遇到点问题 。
- 关于类似Application.Lock的问题,我想实现对多个连接对同一资源的同步
- 考试系统的分数计算应该怎么写?
- 如何让DIV显示在DataGrid上层
- 过客先生们正则高手们我想写个正则一个E-mail的尾是个任意字符的不包括中文()
- 用datalist做购物车.合计问题
- 如何向Repeater的某个单元格中添加一个控件,如何做??
- 思归高手,帮忙吧,汉字乱码问题
- why my iis can't star?
- 执行存储过程生成三个表,如何获取对应信息???
- asp.net页面布局问题
HttpFileCollection files = context.Request.Files;
for (int iFile = 0; iFile < files.Count; iFile++)
{
FileUpload1.SaveAs("你的路径");
}
后台实现循环的代码:
HttpFileCollection files=Request.Files();
for(int i=0;i<files.Count;i++)
{
files[i].SaveAs(Server.MapPat("../upload/img/"+System.IO.Path.GetFileName(files[i].FileName);));
}
FileUpload1 是上传控件吧客户端路径现在还只是存在动态生成的TextBox中,所以问题是怎么加载到上传文件集合中去不通过FileUpload上传方式,还有其它二进制的上传模式现在是只存在客户端文件路径,这路径在上传文件集合中当前是不存在的。
{
string pathName = ((Label)ReptAddFile.Items[i].FindControl("lblFilePath")).Text;
// 读文件:
FileStream str = new FileStream(pathName, FileMode.Open, FileAccess.Read, FileShare.None);
//写文件
FileStream des = new FileStream(Server.MapPath("upload/" +Path.GetFileName(pathName)), FileMode.Create, FileAccess.Write, FileShare.None);
BinaryReader red = new BinaryReader(str);
BinaryWriter wer = new BinaryWriter(des);
try
{
while (true)
{
wer.Write(red.ReadByte()); //做读写操作
}
}
catch
{
wer.Flush();
red.Close();
wer.Close(); }
} 这种方法上传只能限定本地。
你用过一个Flash做的上传控件,怎么不用了呢?
用flash/silverlight/activex是可以做到的
动态生成FILEUPLOAD标记,然后就那么搞了.
Stream StreamObject = UpFile.InputStream; //建立数据流对像文件数据,//FileByteArray为数据储存体,0为数据指针位置、FileLnegth为数据长度
StreamObject.Read(FileByteArray, 0, FileLength);用个静态List<Byte>对象保存所有上传的临时Byte
List<Byte>.Add(FileByTeArray);在选择文件的时候用这种方法先临时保存文件的字节,点击上传的时候在通过获取
List<Byte>[0]字节进行写入操作。。
不知道大家认为这种方法有肯能完成浏览器跟服务器的多文件上传模式吗?
多文件上传的话,要在客户端实现,原理是点击“浏览”选择一个文件后,用js动态产生一个type=file的input,盖在原来的“浏览”按钮上,把原来的藏掉,这样点几次就会有多个type=file的input,点“上传”按钮时一起传到服务器。
很多基于js的多文件上传都是这个做法,看下面的jQuery Ajax file upload,dhtmlxVault,jQuery Multiple File Upload Plugin 和 Ext UploadForm
http://www.open-open.com/ajax/Upload.htm
for (int i = 0; i < ReptAddFile.Items.Count; i++)
{
string pathName = ((Label)ReptAddFile.Items[i].FindControl("lblFilePath")).Text;
pathName = Server.MapPath("upload/" +
Path.GetFileName(pathName).Replace(Path.GetFileName(pathName).Substring(0, Path.GetFileName(pathName).LastIndexOf('.')), GetDateTimeString()));
HttpPostedFile postFile = list[i];
postFile.SaveAs(pathName); }