说明(2017-10-8 23:03:43):

1. 后面的内容都是一些杂七杂八的,零零碎碎的,之前都直接略过了,不过其实还是挺重要的,这次重新学习要认认真真敲一遍。

2. 明天中午9号要回北京了,今晚跟介绍的妹子聊了一个多小时,哎!

3. 下集预告,给图片加水印!

index.html

 <!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>
<title></title>
</head>
<body>
<form action="index.ashx" method="post" enctype="multipart/form-data">
<input type="file" name="file" />
<input type="text" name="txt" />
<input type="submit" value="上传" />
</form>
</body>
</html>

index.ashx

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO; namespace _02_图片上传
{
/// <summary>
/// index 的摘要说明
/// </summary>
public class index : IHttpHandler
{ public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/html";
//接收文件数据
HttpPostedFile file = context.Request.Files["file"];
if (file == null)
{
context.Response.Write("未上传图片!");
}
else
{ //string fileName = Path.GetFileName(file.FileName);
//加上全局唯一标识,防止图片重名
Guid g = Guid.NewGuid();
string fileName = Guid.NewGuid().ToString();
//获取扩展名
string fileExt = Path.GetExtension(file.FileName);
//相对路径
string filePath = "image/" + DateTime.Now.Year + "/" + DateTime.Now.Month + "/" + DateTime.Now.Day + "/";
//绝对路径
//创建文件夹,以年月日命名
string fileRealPath = context.Request.MapPath("image/") + DateTime.Now.Year + "/" + DateTime.Now.Month + "/" + DateTime.Now.Day + "/";
Directory.CreateDirectory(fileRealPath);
//判断文件类型,防止上传病毒
if (fileExt == ".jpg")
{
file.SaveAs(fileRealPath + fileName + fileExt);
//这个地方的图片路径需要是相对路径,不然不显示
context.Response.Write("<html><head></head><body><h1>哈哈哈哈</h1><img src='" + filePath + fileName + fileExt + "'/></body></html>");
} }
} public bool IsReusable
{
get
{
return false;
}
}
}
}

ASP.NET学习笔记(4)——上传图片的更多相关文章

  1. Asp.Net 学习笔记(IIS不同版本和Asp.Net)

    主要目的是在网上记录一下学习笔记,如有不对,请指出 谢谢!! iis5.x: 存在问题,inet info收到动态请求后,aspnt_isapi.dll会被加载到inetinfo.exe(挂载w3sv ...

  2. ASP.NET 学习笔记(一)ASP.NET 概览

    ASP.NET 是一个使用 HTML.CSS.JavaScript 和服务器脚本创建网页和网站的开发框架. ASP.NET 支持三种不同的开发模式:Web Pages(Web 页面).MVC(Mode ...

  3. <转>ASP.NET学习笔记之MVC 3 数据验证 Model Validation 详解

    MVC 3 数据验证 Model Validation 详解  再附加一些比较好的验证详解:(以下均为引用) 1.asp.net mvc3 的数据验证(一) - zhangkai2237 - 博客园 ...

  4. ASP.NET学习笔记--自己写的Login.aspx

    以前有大学有学过,但是没学好,现在准备完全自己动手做一个网站,学习一下ASP.NET 做一个登录页面,首先要有创建一个新的网站,添加Login.aspx,然后做出自己想要的DIV和CSS布局, 之后创 ...

  5. 淘淘商城学习笔记 之 上传图片到远程服务器,图片的回显出现的bug

    最近在学习淘淘商城中用到的技术,感觉受益良多,遇到一个比较奇怪的bug调了好久,遂心乐之分享于诸君 bug情况是这样的:在商城的后台上传图片之后图片回显不出来,右键查看链接,发现链接被加了localh ...

  6. ASP.NET学习笔记(5)——原生Ajax基本操作

    说明(2017-11-4 15:32:49): 1. 回北京后又快一个月了,上次在家写的下回预告,到底是没把加水印写完,而且这次也不想写.. 2. 上次许的愿,十月份看完asp.net,已经泡汤了,翻 ...

  7. ASP.NET学习笔记(3)——用户增删改查(三层)

    说明(2017-10-6 11:21:58): 1. 十一放假在家也没写几行代码,本来还想着利用假期把asp.net看完,结果天天喝酒睡觉,回去的票也没买到,惨.. 2. 断断续续的把用户信息的页面写 ...

  8. ASP代码审计学习笔记 -3.上传漏洞

    1.ASP上传过程抓包分析: POST /4.asp HTTP/1.1 Host: 192.168.1.102 User-Agent: Mozilla/5.0 (Windows NT 10.0; WO ...

  9. ASP.NET学习笔记1—— MVC

    MVC项目文件夹说明 1.App_Data:用来保存数据文件 2.App_Start:包含ASP.NET-MVC系统启动的相关类文件 3.Controllers:存放整个项目"控制器&quo ...

随机推荐

  1. Centos 安装GIT 1.7.1

    在Linux上安装Git 1.首先,你可以试着输入git,看看系统有没有安装Git: git 2.安装GIT https://git-scm.com/download/linux yum instal ...

  2. please verify the preference field with the prompt:Tomcat JDK name

    使用MyEclipse的Tomcat的时候出现下面的问题: a   configuration   error   occurred   during   startup.   please   ve ...

  3. Debian下的crontab保存

    nano编译器 ctrl+x然后y,保存退出

  4. 常用的代码之一:用StopWatch计算代码运行花费的时间。

    先引用Diagnostics using System.Diagnostics; 然后: Stopwatch stopWatch = new Stopwatch(); stopWatch.Start( ...

  5. 使用cronolog自动分割apache的日志。

    为了方便分析网站的日志,需要将apache的日志按周来分割,网上搜索了下,cronolog可以完成这个功能,实际操作了下,操作过程中参考了这篇文章(http://www.lampbo.org/linu ...

  6. sublime在混杂的log数据中提取你想要的内容

    前几天因为同事一个sql写的有问题,导致我这边处理mysql入库出现数据丢失,没什么办法啊,为了回复数据,只能去翻前两天的log了,但是怎么从十几个几十兆的文件中找到我们需要的数据然后提取出来呢,我的 ...

  7. sublime unityshaderplugin

  8. processing fill()和stroke()函数

    在procesiing有两个基本的函数,fill()和stroke()函数,这两个函数分别用来控制形状填充颜色和形状轮廓的颜色,fill()和stroke()可以接受的参数的个数为1,2,3.当参数的 ...

  9. 给Elasticsearch 5.2.2 设置用户权限 how to setting security for elasticsearch on windows

    1. download the plugin of elasticsearch: 下载 readonlyrest-1.14.0_es5.2.2.zip 2. install readonlyrest ...

  10. Linux创建系统用户

    #!/bin/bash users_home_front_dir="/data/users/" ssh_user=$1 user_group=$2 server_user_path ...