步步为营-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环境中文件的上传和下载进行详细的讲解. 一.文件上传 多数文件上传都是通过表单 ...
随机推荐
- python---windows下安装和使用memcache
windows版本下memcache地址http://www.runoob.com/memcached/window-install-memcached.html 注意当选择版本>=1.45时需 ...
- springboot环境下配置过滤器和拦截器
以前我们在配置过滤器和拦截器的时候,都是一个类继承一个接口,然后在xml中配置一下就ok 但是,但是,这是springboot的环境,没有xml的配置.所以我们还要继续学习啊啊啊啊啊~~~~~ 先简单 ...
- filebeat多个key
filebeat.prospectors:- type: log paths: - D:\logs\iis\W3SVC2\*.log exclude_lines: ['^#'] multiline: ...
- nodejs实现新闻爬虫
作为费德勒的铁杆粉丝,每天早上都会在新浪体育里面的网球频道浏览费德勒新闻.由于只关注费德勒的新闻,所以每次都要在网页中大量的新闻中筛选相关信息,感觉效率好低,所以用node写了一个简单的爬虫程序通过每 ...
- ThreadLocal以及内存泄漏
ThreadLocal是什么 ThreadLocal 的作用是提供线程内的局部变量,这种变量在线程的生命周期内起作用,减少同一个线程内多个函数或者组件之间一些公共变量的传递的复杂度.但是如果滥用Thr ...
- 关于javaBean,pojo,EJB
JavaBean是公共Java类 1.所有属性为private.2.提供默认无参构造方法.3.类属性通过getter和setter操作.4.实现serializable接口.
- android 使用get和post将数据提交到服务器
1.activity_main.xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android& ...
- Postfix 邮件服务 - dovecot 服务
dovecot 是一个开源的IMAP和POP3邮件服务器 收件协议 (SMTP 传输发件)POP/IMAP 是MUA从邮件服务器中读取邮件时使用的协议.其中,与POP3是从邮件服务器中下载邮件存起来, ...
- JavaScript动态修改CSS
链接:https://www.cnblogs.com/aademeng/articles/6279060.html 在很多情况下,都需要对网页上元素的样式进行动态的修改.在JavaScript中提供几 ...
- POJ3177 Redundant Paths【双连通分量】
题意: 有F个牧场,1<=F<=5000,现在一个牧群经常需要从一个牧场迁移到另一个牧场.奶牛们已经厌烦老是走同一条路,所以有必要再新修几条路,这样它们从一个牧场迁移到另一个牧场时总是可以 ...