博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
简单的上传图片文件
阅读量:4705 次
发布时间:2019-06-10

本文共 1362 字,大约阅读时间需要 4 分钟。

前台页面:

<!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>
<script src="jQuery.1.7.1.1/Content/Scripts/jquery-1.7.1.js" type="text/javascript"></script>
</head>
<body>
<form method="POST" enctype="multipart/form-data" action="Upload.ashx">
<input type="file" name="imgFile" />
<input type="submit" value="上传"/>
</form>
</body>
</html>

后台的Upload.ashx处理界面代码:

using System;

using System.Collections.Generic;
using System.Drawing.Imaging;
using System.Linq;
using System.Web;
using System.IO;
using System.Drawing;
namespace 新闻小案例._2015_4_19
{
/// <summary>
/// Upload 的摘要说明
/// </summary>
public class Upload : IHttpHandler
{

public void ProcessRequest(HttpContext context)

{
context.Response.ContentType = "text/html";

//获取前台请求的文件
HttpPostedFile file1 = context.Request.Files["imgFile"];
//指定类型为图片类型
string file2 = Path.GetExtension(file1.FileName);
if (file2 == ".jpg" || file2 == ".png")
{
string path1 = "/2015-4-19/" + Guid.NewGuid() + file1.FileName;
file1.SaveAs(context.Request.MapPath(path1));
string str1 = string.Format("<html><head></head><body><img src='{0}'/></body></html>", path1);
context.Response.Write(str1);
}
else
{
context.Response.Write("格式不符");
}

}

}

转载于:https://www.cnblogs.com/luotongchao/p/4440301.html

你可能感兴趣的文章
C#和JAVA 访问修饰符
查看>>
小甲鱼OD学习第1讲
查看>>
HDU-1085 Holding Bin-Laden Captive-母函数
查看>>
php提示undefined index的几种解决方法
查看>>
LRJ
查看>>
Struts2环境搭建
查看>>
Linux: Check version info
查看>>
stl学习之测试stlen,cout等的运行速度
查看>>
魔戒三曲,黑暗散去;人皇加冕,光明归来
查看>>
Error和Exception
查看>>
Python和Singleton (单件)模式[转载]
查看>>
httpclient设置proxy与proxyselector
查看>>
IT常用单词
查看>>
拓扑排序
查看>>
NYOJ--32--SEARCH--组合数
查看>>
gulpfile 压缩模板
查看>>
【34.14%】【BZOJ 3110】 [Zjoi2013]K大数查询
查看>>
【 henuacm2016级暑期训练-动态规划专题 A 】Cards
查看>>
第五篇:白话tornado源码之褪去模板的外衣
查看>>
设备常用框架framework
查看>>