1.  上传文件前台页面

            <div style="padding-left:20px;">
<asp:FileUpload ID="FileUpload1" runat="server" style="width:400px;"/>
<asp:Button ID="Button1" runat="server" OnClick="Upload_Click" Text="上传" style="width:65px;height:22px"></asp:Button >
</div>

  上传文件后台代码

     private string _directory = @"../Questionnaire35"; 

     protected void Upload_Click(object sender, EventArgs e)
{
try
{
if (Request.Files.Count > )
{
if (string.IsNullOrEmpty(Request.Files[].FileName))
{
return;
}
//判断文件大小
int length = Request.Files[].ContentLength;
if (length > )
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script>alert('文件大于1M,不能上传');</script>");
return;
} string type = Request.Files[].ContentType;
string fileExt = Path.GetExtension(Request.Files[].FileName).ToLower();
//只能上传图片,过滤不可上传的文件类型
string fileFilt = ".xlsx|.xls|.docx|.doc|......";
if (fileFilt.IndexOf(fileExt) <= -)
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script>alert('只能上传文档');</script>");
return;
}
else
{
UserInfo user = (UserInfo)Session["UserInfo"];
//string fileName = Server.MapPath(_directory) + "\\" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + fileExt;
string fileName = Server.MapPath(_directory) + "\\" + user.Pkid + fileExt;
Request.Files[].SaveAs(fileName);
Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script>alert('上传成功');</script>");
Answer answer = (Answer)Session["Answer"];
answer.Answer35 = "done";
}
}
}
catch
{
throw new Exception();
}
}

  需要引入 using System.IO;

2. 下载文件前台页面

 <asp:LinkButton runat="server" ID="DownLoadQuestionnaire" Text="下载文件" OnClick="DownLoadQuestionnaire_Click" />

下载文件后台代码

     protected void DownLoadQuestionnaire_Click(object sender, EventArgs e)
{
Response.ContentType = "application/x-zip-compressed"; string docName = "中高职衔接院校调研问卷.docx"; //文件路径
docName = HttpUtility.UrlEncode(docName, System.Text.Encoding.UTF8);
Response.AddHeader("Content-Disposition", "attachment;filename=" + docName);
string filename = Server.MapPath("DownLoad/中高职衔接院校调研问卷.docx");
//指定编码 防止中文文件名乱码
Response.HeaderEncoding = System.Text.Encoding.GetEncoding("GB2312");
Response.TransmitFile(filename); }

  值得注意的就是文件名设置一下编码,不然出现下载时文件名乱码:

  HttpUtility.UrlEncode(docName, System.Text.Encoding.UTF8);

  据说还有很多种下载方式,没一个一个的试验:

     protected void DownLoadQuestionnaire_Click(object sender, EventArgs e)
{
string fileName = "asd.txt";//客户端保存的文件名
string filePath = Server.MapPath("DownLoad/aaa.txt");//路径 FileInfo fileInfo = new FileInfo(filePath);
Response.Clear();
Response.ClearContent();
Response.ClearHeaders();
Response.AddHeader("Content-Disposition", "attachment;filename=" + fileName);
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(); } protected void DownLoadQuestionnaire_Click(object sender, EventArgs e)
{
string fileName = "aaa.txt";//客户端保存的文件名
string filePath = Server.MapPath("DownLoad/aaa.txt");//路径
System.IO.FileInfo fileInfo = new System.IO.FileInfo(filePath);
if (fileInfo.Exists == true)
{
const long ChunkSize = ;//100K 每次读取文件,只读取100K,这样可以缓解服务器的压力
byte[] buffer = new byte[ChunkSize]; Response.Clear();
System.IO.FileStream iStream = System.IO.File.OpenRead(filePath);
long dataLengthToRead = iStream.Length;//获取下载的文件总大小
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName));
while (dataLengthToRead > && Response.IsClientConnected)
{
int lengthRead = iStream.Read(buffer, , Convert.ToInt32(ChunkSize));//读取的大小
Response.OutputStream.Write(buffer, , lengthRead);
Response.Flush();
dataLengthToRead = dataLengthToRead - lengthRead;
}
Response.Close();
}
} protected void DownLoadQuestionnaire_Click(object sender, EventArgs e)
{
string fileName = "aaa.txt";//客户端保存的文件名
string filePath = Server.MapPath("DownLoad/aaa.txt");//路径 //以字符流的形式下载文件
FileStream fs = new FileStream(filePath, FileMode.Open);
byte[] bytes = new byte[(int)fs.Length];
fs.Read(bytes, , bytes.Length);
fs.Close();
Response.ContentType = "application/octet-stream";
//通知浏览器下载文件而不是打开
Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));
Response.BinaryWrite(bytes);
Response.Flush();
Response.End();
}

C# ASP 上传/下载文件的更多相关文章

  1. rz和sz上传下载文件工具lrzsz

    ######################### rz和sz上传下载文件工具lrzsz ####################################################### ...

  2. linux上很方便的上传下载文件工具rz和sz

    linux上很方便的上传下载文件工具rz和sz(本文适合linux入门的朋友) ##########################################################&l ...

  3. shell通过ftp实现上传/下载文件

    直接代码,shell文件名为testFtptool.sh: #!/bin/bash ########################################################## ...

  4. SFTP远程连接服务器上传下载文件-qt4.8.0-vs2010编译器-项目实例

    本项目仅测试远程连接服务器,支持上传,下载文件,更多功能开发请看API自行开发. 环境:win7系统,Qt4.8.0版本,vs2010编译器 qt4.8.0-vs2010编译器项目实例下载地址:CSD ...

  5. linux下常用FTP命令 上传下载文件【转】

    1. 连接ftp服务器 格式:ftp [hostname| ip-address]a)在linux命令行下输入: ftp 192.168.1.1 b)服务器询问你用户名和密码,分别输入用户名和相应密码 ...

  6. C#实现http协议支持上传下载文件的GET、POST请求

    C#实现http协议支持上传下载文件的GET.POST请求using System; using System.Collections.Generic; using System.Text; usin ...

  7. HttpClient上传下载文件

    HttpClient上传下载文件 java HttpClient Maven依赖 <dependency> <groupId>org.apache.httpcomponents ...

  8. 初级版python登录验证,上传下载文件加MD5文件校验

    服务器端程序 import socket import json import struct import hashlib import os def md5_code(usr, pwd): ret ...

  9. 如何利用京东云的对象存储(OSS)上传下载文件

    作者:刘冀 在公有云厂商里都有对象存储,京东云也不例外,而且也兼容S3的标准因此可以利用相关的工具去上传下载文件,本文主要记录一下利用CloudBerry Explorer for Amazon S3 ...

随机推荐

  1. vue 表格使用el-select

    <el-table-column label="示例" width="210" align="center"> <temp ...

  2. 数字货币交易所(火币为例)如何使用二次验证码/虚拟MFA/两步验证/谷歌验证器?

    一般点账户名——设置——安全设置中开通虚拟MFA两步验证 具体步骤见链接  数字货币交易所(火币为例)如何使用二次验证码/虚拟MFA/两步验证/谷歌验证器? 二次验证码小程序于谷歌身份验证器APP的优 ...

  3. flask下直接展示mysql数据库 字段

    在工作中,会导出一份mysql的html来查看,用的是就是路过秋天大神的那个工具,所以想自己用那个样式直接在后端写一个页面做展示! 前端页面 from flask import Flask,reque ...

  4. vue图片上传的简单组件

    <template> <div class="rili" id="rili"> <div class="updel&qu ...

  5. 扫描PDF417崩溃的原因找到:手机摄像头分辨率低

    换孩子姥姥华为手机解决了. 能扫pdf417码了

  6. Flask框架(一):介绍与环境搭建

    1.Flask介绍 Flask诞生于2010年,是Armin ronacher(人名)用 Python 语言基于 Werkzeug 工具箱编写的轻量级Web开发框架. Flask 本身相当于一个内核, ...

  7. Python之生成器、迭代器

    生成器 生成器类似返回值为数组的一个函数,这个函数可以接受参数,可被调用,但只能产生一个值,所以大大节省内存. 生成器表达式的语法非常简单,只需要将列表推导式的中括号改成小括号就可以了 [x+x fo ...

  8. AList的具体实现 #CS61B-sp18-2.5

    实现一个Array based list,其功能包括获取长度size,添加元素至最后addLast,得到元素get和去除最后一个元素. 设计思路及其实现: 我们都知道在获取数据的时候,直接调用缓存里面 ...

  9. Lua学习入门(代码块)

    ). if then else if a < then b = else b = end ). if elseif else then if a < then b = elseif a = ...

  10. 第十章 函数式接口&Stream流

    10.1.函数式接口 10.1.1.概述 有且仅有一个抽象方法的接口,并且可以通过在类上标注@FunctionalInterface注解进行检测,建议自定义的函数式接口都加上这个注解 10.1.2.函 ...