大文件的上传一般通过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简单练习(文件的上传和下载)的更多相关文章

  1. asp.net web开发——文件的上传和下载

    HTML部分 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="index.a ...

  2. 简单的文件ftp上传

    目录 简单的文件ftp上传 简单的文件ftp上传 server import socket import struct service=socket.socket() service.bind(('1 ...

  3. JavaWeb中文件的上传和下载

    JavaWeb中文件的上传和下载 转自: JavaWeb学习总结(五十)——文件上传和下载 - 孤傲苍狼 - 博客园https://www.cnblogs.com/xdp-gacl/p/4200090 ...

  4. Apache FtpServer 实现文件的上传和下载

    1 下载需要的jar包 Ftp服务器实现文件的上传和下载,主要依赖jar包为: 2 搭建ftp服务器 参考Windows 上搭建Apache FtpServer,搭建ftp服务器 3 主要代码 在ec ...

  5. 初学Java Web(7)——文件的上传和下载

    文件上传 文件上传前的准备 在表单中必须有一个上传的控件 <input type="file" name="testImg"/> 因为 GET 方式 ...

  6. java web(四):request、response一些用法和文件的上传和下载

    上一篇讲了ServletContent.ServletCOnfig.HTTPSession.request.response几个对象的生命周期.作用范围和一些用法.今天通过一个小项目运用这些知识.简单 ...

  7. java实现文件的上传和下载

    1. servlet 如何实现文件的上传和下载? 1.1上传文件 参考自:http://blog.csdn.net/hzc543806053/article/details/7524491 通过前台选 ...

  8. Spring MVC 实现文件的上传和下载

    前些天一位江苏经贸的学弟跟我留言问了我这样一个问题:“用什么技术来实现一般网页上文件的上传和下载?是框架还是Java中的IO流”.我回复他说:“使用Spring MVC框架可以做到这一点,因为Spri ...

  9. 文件的上传和下载--SpringMVC

    文件的上传和下载是项目开发中最常用的功能,例如图片的上传和下载.邮件附件的上传和下载等. 接下来,将对Spring MVC环境中文件的上传和下载进行详细的讲解. 一.文件上传 多数文件上传都是通过表单 ...

随机推荐

  1. java内存模型及内存与cpu之间的关系

    主内存和cpu之间的关系,因为cpu是在是处理速度太快了.所以一般cpu都有一个cpu缓存,上图的意思是主内存--->cpu缓存--->cpu寄存器--->cpu执行处理,写的时候反 ...

  2. Hive记录-部署Hive环境

    1.配置 hive1.2.0(前提要配置hadoop2.7.2,前面文档有介绍) #官网下载二进制包,解压到/usr/app 下,配置/etc/profile: export HIVE_HOME=/u ...

  3. C#多线程和异步(一)——基本概念和使用方法

    一.多线程相关的基本概念 进程(Process):是系统中的一个基本概念. 一个正在运行的应用程序在操作系统中被视为一个进程,包含着一个运行程序所需要的资源,进程可以包括一个或多个线程 .进程之间是相 ...

  4. MongoDB 时差问题问题

    在读取的时候,需要再次转换回来,比较麻烦. 其实,Mongo本身就已经提供了相应的处理方法,即在实体类中加个属性即可.具体如下: [BsonDateTimeOptions(Kind = DateTim ...

  5. div背景透明内容不透明与0.5PX边框兼容设置

    1.问题:设置 border-width:0.5px;  并兼容安卓和苹果移动端.  兼容:苹果IOS的 safari 支持浮点数边框,安卓浏览器不支持,会四舍五入到1px.不同浏览器效果额不同  解 ...

  6. C# HTTP上传多个文件及传递参数

    1.HTTP上传文件及传递参数 #region 6.0 上传多个文件和参数 /// <summary> /// HttpUploadFile /// </summary> // ...

  7. VMware workstation 网络选择 NAT模式 访问外网

    多年不用本地做测试  尽然被 nat 模式給卡着了  :动手的还是所以要记录一下: 1.根据自己需求创建 虚拟机 之后: 配置[网络适配器] -- 选择 nat 模式 ( 选择网卡 )   虚拟机   ...

  8. C++使用目录

    VS2017的安装和配置 常用指令 C++ 数据类型   常量 运算符 数组 字符串  Ansi与Unicode  指针   模态与非模态对话框  变量的引用& new和delete动态分配和 ...

  9. charCodeAt() 和charAt()

    charAt() 方法可返回指定位置的字符. charCodeAt() 方法可返回指定位置的字符的 Unicode 编码.这个返回值是 0 - 65535 之间的整数. 方法 charCodeAt() ...

  10. 基于json文件实现的gearman任务自动重启

    一:在gearman任务失败后,调用task_failed def task_failed(task, *args): info = '\n'.join(args) datetime = local_ ...