ASP.NET FileUpload以及文件下载和Repeater控件显示
来源:互联网 发布:虾米音乐 2.0mac 编辑:程序博客网 时间:2024/05/22 03:04
WebForm1.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="文件夹上传以及Repeater.WebForm1" %><!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head runat="server"><meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title></title></head><body> <form id="form1" runat="server"> <div> <asp:FileUpload ID="FileUpload1" runat="server" /> <asp:Button runat="server" ID="btnUpload" Text="上传" OnClick="btnUpload_Click" /> </div> <div> 文件下载:<br /> <asp:Repeater runat="server" ID="repeater1" > <HeaderTemplate> <table border="1"> <tr> <td>文件顺序</td><td>文件下载</td> </tr> </HeaderTemplate> <ItemTemplate> <tr> <td><%# Container.ItemIndex+1 %></td> <td><asp:LinkButton runat="server" ID="linkbtnDownloda" Text='<%# Eval("filenames") %>' CommandArgument='<%#Eval("filepath") %>' OnCommand="linkbtnDownloda_Command"/> </td> </tr> </ItemTemplate> <FooterTemplate> </table> </FooterTemplate> </asp:Repeater> </div> </form></body></html>WebForm1.aspx.cs
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;using System.IO;using Model;using BLL;namespace 文件夹上传以及Repeater{ public partial class WebForm1 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { Bind(); } } public void Bind() { this.repeater1.DataSource = BLL.FileManager.selectAllFile().Tables[0]; this.repeater1.DataBind(); } /// protected void btnUpload_Click(object sender, EventArgs e) { string fullfilename=this.FileUpload1.PostedFile.FileName; string filename = fullfilename.Substring(fullfilename.LastIndexOf("\\") + 1); string filetype = fullfilename.Substring(fullfilename.LastIndexOf(".") + 1); Response.Write("文件名:"+filename+",类型:"+filetype); ////// string savepath = Server.MapPath("~\\Files\\" + filename); this.FileUpload1.SaveAs(savepath); // Model.FileInfo fi = new Model.FileInfo(); fi.Filepath = "~\\Files\\"+filename; fi.Filenames = filename; BLL.FileManager.uploadFile(fi); Bind(); } protected void linkbtnDownloda_Command(object sender, CommandEventArgs e) { string uri = e.CommandArgument.ToString(); string filename = uri.Substring(uri.LastIndexOf("\\") + 1); //Response.Write(uri); FileStream fs = new FileStream(Server.MapPath(uri), FileMode.Open); byte[] bytes = new byte[(int)fs.Length]; fs.Read(bytes, 0, (int)fs.Length); //用完记得关闭字符流操作 fs.Close(); //设置当前字符流在HTTP上输出的格式为stream字符流格式,固定写法 Response.ContentType = "application/octet-stream"; //通知浏览器进行下载操作,并保存当前下载的文件名到本地 Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(filename, System.Text.Encoding.UTF8)); //Response.AddHeader("Content-Disposition", "attachment; filename=" + filename); //将HTTP中的字符流做写入缓冲操作 Response.BinaryWrite(bytes); //将当前HTTP缓冲中保存的字符流写入到本地 Response.Flush(); //写入完成后结束整个操作 Response.End(); } }}
阅读全文
0 0
- ASP.NET FileUpload以及文件下载和Repeater控件显示
- ASP.NET Repeater以及分页控件AspNetPager
- ASP.NET用Repeater控件显示数据
- ASP.NET用Repeater控件显示数据
- Asp.net用Repeater控件显示数据
- Asp.net中用FileUpload控件上传文件
- Asp.net 2.0 用 FileUpload 控件实现多文件上传 用户控件(示例代码下载)
- Asp.net 2.0 用 FileUpload 控件实现多文件上传 用户控件(示例代码下载)
- Asp.net 2.0 用 FileUpload 控件实现多文件上传 用户控件(示例代码下载)
- Asp.net 2.0 用 FileUpload 控件实现多文件上传 用户控件(示例代码下载)
- Asp.net 2.0 用 FileUpload 控件实现多文件上传 用户控件(示例代码下载)
- Asp.net 2.0 用 FileUpload 控件实现多文件上传 用户控件(示例代码下载)
- Asp.net 2.0 用 FileUpload 控件实现多文件上传 用户控件(示例代码下载)
- Asp.net 2.0 用 FileUpload 控件实现多文件上传 用户控件(示例代码下载)
- Asp.net 2.0 用 FileUpload 控件实现多文件上传 用户控件(示例代码下载)
- Asp.net 2.0 用 FileUpload 控件实现多文件上传 用户控件(示例代码下载)
- ASP.NET的DataList和Repeater控件
- ASP.NET Repeater控件
- java8之方法引用
- 百分百屏幕适配(性能略低)
- 高效sql性能优化极简教程
- mysql在linux下安装
- EMNLP 2017 accepted papers
- ASP.NET FileUpload以及文件下载和Repeater控件显示
- PHPとSQLserver接続
- SpringBoot非官方教程 | 第二十二篇: 创建含有多module的springboot工程
- 硅胶制品为何丝印后字符会掉?
- vue的export default和new vue({})
- 《算法导论》学习笔记之Chapter4.2矩阵乘法Strassen
- ServletContext学习
- 阿里云Linux下MySQL5.7.20安装
- POJ-2455