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 ...
随机推荐
- javaWEB总结(3):ServletConfig对象
1.首先找到ServletConfig的API: ServletConfig封装了servlet的配置信息,并且可以获取servletContext对象. ServletConfig共有四个方法: 1 ...
- HIT Winter Day ACM入门
A. Arpa’s hard exam and Mehrdad’s naive cheat 题意:统计1378^n的末尾数字 即统计8^n的末尾数字 n=0时为1 其他情况为{8,4,2,6}中的一个 ...
- Chapter 2 Open Book——13
"People in this town," he muttered. "Dr. Cullen is a brilliant surgeon who could prob ...
- diff and patch
A patch captures the changes of two different files (oldfile and newfile). Given the oldfile and the ...
- UIImageView的UserInteractionEnabled什么时候为no
UIImageView作为背景,但直接把按钮或者UITextField放在上面无法相应事件 特殊子类的覆盖 userInteractionEnabled属性默认值为YES,但UIView的一些子类中对 ...
- http response
关键词:http response header 下载文件 案例1: 访问某个链接,然后下载文件,需要特定的http头: header("Content-Type:application/z ...
- Docker学习笔记 — Docker私有仓库搭建【转载】
标签: Docker 2015-03-10 21:08 24190人阅读 评论(0) 收藏 举报 分类: Docker(26) 目录(?)[+] 和Mavan的管理一样,Dockers不仅 ...
- listview前几个item怎么不停加载
在加载前几个item的时候,listview有个Adapter,里面的getView方法会被调用好几遍.原因可能有两种: 1.listview在布局文件里高度写成了wrap_content <? ...
- 转:用ANT执行SQL
http://blog.chinaunix.net/xmlrpc.php?r=blog/article&uid=21340438&id=5160076 http://kayo.itey ...
- php mysql 实现消息队列
最近遇到一个批量发送短信的需求,短信接口是第三方提供的.刚开始想到,获取到手机号之后,循环调用接口发送不就可以了吗? 但很快发现问题:当短信数量很大时,不仅耗时,而且成功率很低. 于是想到,用PHP和 ...