首页 > Web开发 > 详细

简单的上传图片文件

时间:2015-04-20 00:05:14      阅读:318      评论:0      收藏:0      [点我收藏+]

前台页面:

<!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("格式不符");
}

}

}

简单的上传图片文件

原文:http://www.cnblogs.com/luotongchao/p/4440301.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!