步步为营-70-asp.net简单练习(文件的上传和下载)
大文件的上传一般通过FTP协议,而一般小的文件可以通过http协议来完成
1 通过asp.net 完成图片的上传
1.1 创建html页面
注意:1 method="post" ;2 enctype="multipart/form-data"; 3 <input type="file" />
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
</head>
<body>
<form method="post" action="FileUpload.ashx" enctype="multipart/form-data">
<input type="file" id="imgUpLoad" name="imgUpLoad" />
<input type="submit" value="提交" />
</form>
</body>
</html>
FileUpload.html
1.2 创建一般处理程序.ashx
注意:1 创建文件保存路径
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web; namespace _05_文件的上传与下载
{
/// <summary>
/// FileUpload 的摘要说明
/// </summary>
public class FileUpload : IHttpHandler
{ public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/html"; //01 获取文件
HttpPostedFile pf = context.Request.Files["imgUpLoad"];
//02 创建文件保存路径
string savePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory+"Upload/"+pf.FileName);
//03 保存文件
pf.SaveAs(savePath);
//04 显示上传的文件
context.Response.Write("<img src='Upload/"+pf.FileName+"'/> ");
} public bool IsReusable
{
get
{
return false;
}
}
}
}
FileUpload.ashx

2 上传文件格式的验证,假设规定只能上传,gif的图片
我们可以在HTML通过jQuery来进行验证,也可以在.ashx中进行验证
2.1 修改ashx文件
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web; namespace _05_文件的上传与下载
{
/// <summary>
/// FileUpload 的摘要说明
/// </summary>
public class FileUpload : IHttpHandler
{ public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/html"; //01 获取文件
HttpPostedFile pf = context.Request.Files["imgUpLoad"];
//01-01 获取文件后缀名
string extName = pf.FileName.Substring(pf.FileName.LastIndexOf('.'));
if (extName != ".gif" || extName != ".Gif")
{
context.Response.Write("请上传.gif图片");
return;
}
//02 创建文件保存路径
string savePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory+"Upload/"+pf.FileName);
//03 保存文件
pf.SaveAs(savePath);
//04 显示上传的文件
context.Response.Write("<img src='Upload/"+pf.FileName+"'/> ");
} public bool IsReusable
{
get
{
return false;
}
}
}
}
ashx

2.2 引入jQuery,修改HTML页面
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<script src="http://localhost:62225/Script/jquery-1.7.1.min.js"></script>
<title></title>
<script>
$(function () {
$("form").submit(function () {
var fname = $("#imgUpLoad").val();
var extname = fname.substring(fname.lastIndexOf('.'));
if (extname != ".gif" || extname != ".Gif") {
alert("请上传.gif图片");
return false;
} });
});
</script>
</head>
<body>
<form method="post" action="FileUpload.ashx" enctype="multipart/form-data">
<input type="file" id="imgUpLoad" name="imgUpLoad" />
<input type="submit" value="提交" />
</form>
</body>
</html>
html

3 如果文件只放在Upload文件夹下,随着时间的增长,文件势必会越来越多不利于寻找,可以根据日期建立相应文件夹
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web; namespace _05_文件的上传与下载
{
/// <summary>
/// FileUpload 的摘要说明
/// </summary>
public class FileUpload : IHttpHandler
{ public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/html"; //01 获取文件
HttpPostedFile pf = context.Request.Files["imgUpLoad"];
//01-01 获取文件后缀名
string extName = pf.FileName.Substring(pf.FileName.LastIndexOf('.'));
if (extName != ".gif" && extName != ".GIF")
{
context.Response.Write("请上传.gif图片");
return;
}
//02 创建文件保存路径
string savePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory+"Upload\\");
//02-01 根据日期创建文件夹
DateTime dt = DateTime.Now;
savePath += dt.Year + "\\" + dt.Month + "\\" + dt.Day ;
if (!Directory.Exists(savePath))
{
//创建文件夹
Directory.CreateDirectory(savePath);
}
//02-02文 件名为当前时间 savePath += "\\"+ dt.ToString().Replace(':','-')+".gif";
//03 保存文件
pf.SaveAs(savePath);
//04 显示上传的文件
context.Response.Write("<img src='" + savePath.Substring(savePath.IndexOf("Upload")) + "'/> ");
} public bool IsReusable
{
get
{
return false;
}
}
}
}
ashx

4 文件下载
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
</head>
<body>
<img src="" />
<a href="FileDownload.ashx?f=Upload/2017.rar">Upload/.rar</a>
<a href="FileDownload.ashx?f=Upload/2017-06-14%2017-25-19.gif">Upload/--%--.gif</a>
</body>
</html>
html
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web; namespace _05_文件的上传与下载
{
/// <summary>
/// FileDownload 的摘要说明
/// </summary>
public class FileDownload : IHttpHandler
{ public void ProcessRequest(HttpContext context)
{
string f = context.Request["f"];
context.Response.ContentType = "application/octet-stream"; context.Response.AddHeader("Content-Disposition","attachment;filename=\""+f+"\";"); context.Response.WriteFile(Path.Combine(AppDomain.CurrentDomain.BaseDirectory,f));
} public bool IsReusable
{
get
{
return false;
}
}
}
}
ashx

步步为营-70-asp.net简单练习(文件的上传和下载)的更多相关文章
- asp.net web开发——文件的上传和下载
HTML部分 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="index.a ...
- 简单的文件ftp上传
目录 简单的文件ftp上传 简单的文件ftp上传 server import socket import struct service=socket.socket() service.bind(('1 ...
- JavaWeb中文件的上传和下载
JavaWeb中文件的上传和下载 转自: JavaWeb学习总结(五十)——文件上传和下载 - 孤傲苍狼 - 博客园https://www.cnblogs.com/xdp-gacl/p/4200090 ...
- Apache FtpServer 实现文件的上传和下载
1 下载需要的jar包 Ftp服务器实现文件的上传和下载,主要依赖jar包为: 2 搭建ftp服务器 参考Windows 上搭建Apache FtpServer,搭建ftp服务器 3 主要代码 在ec ...
- 初学Java Web(7)——文件的上传和下载
文件上传 文件上传前的准备 在表单中必须有一个上传的控件 <input type="file" name="testImg"/> 因为 GET 方式 ...
- java web(四):request、response一些用法和文件的上传和下载
上一篇讲了ServletContent.ServletCOnfig.HTTPSession.request.response几个对象的生命周期.作用范围和一些用法.今天通过一个小项目运用这些知识.简单 ...
- java实现文件的上传和下载
1. servlet 如何实现文件的上传和下载? 1.1上传文件 参考自:http://blog.csdn.net/hzc543806053/article/details/7524491 通过前台选 ...
- Spring MVC 实现文件的上传和下载
前些天一位江苏经贸的学弟跟我留言问了我这样一个问题:“用什么技术来实现一般网页上文件的上传和下载?是框架还是Java中的IO流”.我回复他说:“使用Spring MVC框架可以做到这一点,因为Spri ...
- 文件的上传和下载--SpringMVC
文件的上传和下载是项目开发中最常用的功能,例如图片的上传和下载.邮件附件的上传和下载等. 接下来,将对Spring MVC环境中文件的上传和下载进行详细的讲解. 一.文件上传 多数文件上传都是通过表单 ...
随机推荐
- Ubuntu 下 Tomcat7 的安装和配置
tomcat下载地址:http://tomcat.apache.org/download-70.cgi 声明:下面的内容和原创笔者的博文的内容差不多,不一样的只是tomcat7的安装目录不同,我按照我 ...
- MySQL数据库中.SQL文件的导出方式
转自:http://tech.watchstor.com/management-117401.htm 在MySQL数据库中导入SQL文件是件很麻烦的事情,但是这是一项大家非常值得学习的技术,本文就从最 ...
- 虚拟机centos7系统下安装hadoop ha和yarn ha(详细)
一:基础环境准备 (一):虚拟机新建五个centos7系统(复制文件夹的方式) (二):角色分配 (三)按照角色分配表配置 (1)更改主机ip(自行查找),如果只是个人搭建玩一玩,可选择安装cento ...
- Asp.net MVC Session过期异常的处理
一.使用MVC中的Filter来对Session进行验证 (1)方法1: public class MyAuthorizeAttribute : FilterAttribute, IAuthoriza ...
- UE4的AI学习(2)——官方案例实例分析
官方给出的AI实例是实现一个跟随着玩家跑的AI,当玩家没有在AI视野里时,它会继续跑到最后看到玩家的地点,等待几秒后如果仍然看不到玩家,则跑回初始地点.官方的案例已经讲得比较详细,对于一些具体的函数调 ...
- 【反射】利用java反射原理将xml文件中的字段封装成对应的Bean
本例使用的xml解析方式为jdom ... <ROOT> <Consignment> ... </Consignment> </ROOT> 解析xml文 ...
- java 两个list 交集 并集 差集 去重复并集
前提需要明白List是引用类型,引用类型采用引用传递. 我们经常会遇到一些需求求集合的交集.差集.并集.例如下面两个集合: List<String> list1 = new ArrayLi ...
- Spring之JDBCTemplate学习
一.Spring对不同的持久化支持: Spring为各种支持的持久化技术,都提供了简单操作的模板和回调 ORM持久化技术 模板类 JDBC org.springframework.jdbc.core. ...
- .Net Core中使用RabbitMQ
(1).引入依赖 RabbitMQ.Client (2).编写发布者代码 var connectionFactory = new ConnectionFactory() { HostName=&quo ...
- (并发编程)进程IPC,生产者消费者模型,守护进程补充
一.IPC(进程间通信)机制进程之间通信必须找到一种介质,该介质必须满足1.是所有进程共享的2.必须是内存空间附加:帮我们自动处理好锁的问题 a.from multiprocessing import ...