aspx 文件上传和下载,多文件上传
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="MultiFileUpload.aspx.cs"
Inherits="MultiFileUpload"%>
<!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>
<script type="text/javascript">
function addFile() {
var div = document.createElement("div");
var f = document.createElement("input");
f.setAttribute("type", "file")
f.setAttribute("name", "File")
f.setAttribute("size", "50")
div.appendChild(f)
var d = document.createElement("input");
d.setAttribute("type", "button")
d.setAttribute("onclick", "deteFile(this)");
d.setAttribute("value", "移除")
div.appendChild(d)
document.getElementById("_container").appendChild(div);
}
function deteFile(o) {
while (o.tagName != "DIV") o = o.parentNode;
o.parentNode.removeChild(o);
}
</script>
</head>
<body>
<form id="form1" runat="server" method="post" enctype="multipart/form-data">
<h3>多文件上传</h3>
用户名:<asp:TextBoxID="TextBox1" runat="server"></asp:TextBox>
<div id="_container">
<input type="file" size="50" name="File"/>
</div>
<div>
<input type="button" value="添加文件(Add)" onclick="addFile()"/>
</div>
<div style="padding:10px 0">
<asp:Buttonrunat="server" Text="开始上传" ID="UploadButton"
onclick="UploadButton_Click"></asp:Button>
</div>
<div>
<asp:Label ID="strStatus" runat="server" Font-Names="宋体" Font-Bold="True" Font-Size="9pt"
Width="500px" BorderStyle="None" BorderColor="White"></asp:Label>
</div>
</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;
publicpartialclass MultiFileUpload : System.Web.UI.Page
{
protectedvoid UploadButton_Click(object sender, EventArgs e)
{
///'遍历File表单元素
HttpFileCollection files = HttpContext.Current.Request.Files;
/// '状态信息
System.Text.StringBuilder strMsg = new System.Text.StringBuilder("您输入的用户名是:" + TextBox1.Text +"<br/>");
strMsg.Append("上传的文件分别是:<hr color='red'/>");
try
{
for (int iFile =0; iFile < files.Count; iFile++)
{
///'检查文件扩展名字
HttpPostedFile postedFile = files[iFile];
string fileName, fileExtension;
fileName = System.IO.Path.GetFileName(postedFile.FileName);
if (fileName !="")
{
fileExtension = System.IO.Path.GetExtension(fileName);
strMsg.Append("上传的文件类型:" + postedFile.ContentType.ToString() +"<br>");
strMsg.Append("客户端文件地址:" + postedFile.FileName +"<br>");
strMsg.Append("上传文件的文件名:" + fileName +"<br>");
strMsg.Append("上传文件的扩展名:" + fileExtension +"<br><hr>");
///'可根据扩展名字的不同保存到不同的文件夹
///注意:可能要修改你的文件夹的匿名写入权限。
postedFile.SaveAs(System.Web.HttpContext.Current.Request.MapPath("images/") + fileName);
}
}
strStatus.Text = strMsg.ToString();
}
catch (System.Exception Ex)
{
strStatus.Text = Ex.Message;
}
}
}
下载
public void Download(string path) //要下载文件的名称
{ 比如string path = "\\images\\a.text";自己检查自己的路径问题,有的需要添加上级目录 有的直接输入文件名就可
if (!string.IsNullOrEmpty(path))
{
string filePath = Server.MapPath("")+path;//路径
// string filePath =@"C:\Users\Public\Pictures\Sample Pictures\1.jpg";
FileInfo fileInfo = new FileInfo(filePath);
Response.Clear();
Response.ClearContent();
Response.ClearHeaders();
Response.AddHeader("Content-Disposition", "attachment;filename=" + fileInfo.Name);
Response.AddHeader("Content-Length", fileInfo.Length.ToString());
Response.AddHeader("Content-Transfer-Encoding", "binary");
Response.ContentType = "application/octet-stream";
Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
Response.WriteFile(fileInfo.FullName);
Response.Flush();
Response.End();
}
}
aspx 文件上传和下载,多文件上传的更多相关文章
- XShell上传、下载本地文件到linux服务器
Python之道发表于程序员八阿哥订阅 1.2K 腾讯云服务器 年付3折起 首次购买云服务器 最低3折起 超高性价比 限时抢购 Xshell很好用,然后有时候想在windows和linux上传或下载某 ...
- php 上传文件实例 上传并下载word文件
上传界面 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3 ...
- Eclipse 上传 删除 下载 分析 hdfs 上的文件
本篇讲解如何通过Eclipse 编写代码去操作分析hdfs 上的文件. 1.在eclipse 下新建Map/Reduce Project项目.如图: 项目建好后,会默认加载一系列相应的jar包. 下 ...
- Linux命令行上传本地文件到服务器 、 下载服务器文件到本地
sh使用命令: scp 将本地文件上传至服务器 第一个是本地文件的路径/文件名, 例如 ./index.tar.gz . index.html . bg.png 等 第二个是要上传到的服务器的位置 ...
- SpringMVC下文件的上传与下载以及文件列表的显示
1.配置好SpringMVC环境-----SpringMVC的HelloWorld快速入门! 导入jar包:commons-fileupload-1.3.1.jar和commons-io-2.4.ja ...
- linux学习 XShell上传、下载本地文件到linux服务器
(一)通过命令行的方式 1.linux服务器端设置 在linux主机上,安装上传下载工具包rz及sz; 如果不知道你要安装包的具体名称,可以使用yum provides */name 进行查找系统自带 ...
- drupal7 开发自定义上传、下载模块的上传功能
关键点有两个:1.在页面上显示出上传的控件,2.代码实现文件上传到服务器的功能 一.显示控件: 先来看关键点1: 实现页面显示出上传控件, 关键代码: $form['my_file_field'] = ...
- .NET环境下上传和下载Word文件
一.上传Word文档或者其他文档 1.简单地上传文件的web服务方法如下 [WebMethod] public void UploadFile() { using (TransactionScope ...
- 使用fastDFS上传和下载图片文件
package com.xuecheng.test.fastdfs;import org.csource.common.MyException;import org.csource.fastdfs.* ...
- Java利用 ganymed-ssh2-build.jar来上传文件到linux以及下载linux文件以及执行linux shell命令
package api; import java.io.BufferedReader;import java.io.InputStreamReader;import java.io.IOExcepti ...
随机推荐
- 用Py2exe打包Python脚本简单介绍
一.简述 Py2exe,从这个名字上就可以理解,把Python脚本转换为windows平台上面可以运行的可执行程序(*.exe)的工具.经过转换后,你可以不 用安装Python的执行环境就可 ...
- javascript 如何正确使用getElementById,getElementsByName(), and getElementsByTagName()
WEB标准下可以通过getElementById(), getElementsByName(), and getElementsByTagName()访问DOCUMENT中的任一个标签. (1)get ...
- 自动加载U盘
编辑/etc/fstab 比如想在开机的时候将/dev/sda1安装在/mnt 可以在/etc/fstab中加入一行 /dev/sda1 /mnt ext3 defaults 0 ...
- flashbuilder mx组件 MenuBar
来源:http://www.cuplayer.com/player/PlayerCode/Flex/2013/0118661.html <fx:Script> <![CDATA[ i ...
- Javascript教程
Javascript教程 laiqun@msn.cn Contents 1. javascript嵌入方式 2. javascript语法 3. 数据类型 4. javascript变量 5. 字符串 ...
- 全选js实现
前端全选框 <input type='checkbox' name='allSelect' onclick='sel(this)'></input> //有i个选择框 < ...
- linux 查看 cpu 和内存的命令 - top
1.查看内存,cpu ,当前进程task数目, 每个进程的cpu, 内存使用率, 用top 命令: 在这个页面,按 P,下面的进程排序,以cpu使用率降序排列. 按M,按内存使用率降序排列: 按N, ...
- 诡异的php 输出缓冲
我的本地环境 windows + apche + php5.2 今天,碰到一个诡异的问题,以前认为 php 脚本中调用 heade()函数之前不能有任何的如 echo,print ,print_r, ...
- php redis 消息队列
redis是什么东西就不多说了,网上文章一搜一大堆. 首先来说一下我要实现的功能: 类似一个消息中转站吧,如果有人要发送消息,先将消息发到我这里来,然后我这边进行转发,为的就是有一个统一的管理和修改时 ...
- CGRect相关工具函数
NSStringFromCGRect(aCGRect): CGRectFromString(aString):如果把视图的框架以字符串的形式放在NSUserDefaults里面,那么该方法可以将其转回 ...