asp.net 简单图片上传
01、TuPianShang.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
</head>
<body>
<form action="/ImageUpload.ashx" method="post" enctype="multipart/form-data">
<input type="file" name="imagesFile" />
<input type="submit" value="上传文件" />
</form>
</body>
</html>
02、新建一般处理程序ImageUpload.ashx
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace CeShiTuPian
{
/// <summary>
/// ImageUpload 的摘要说明
/// </summary>
public class ImageUpload : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
//context.Response.Write("Hello World");
接收表单传递过来的图片
HttpPostedFile file = context.Request.Files["imagesFile"];
//拿到文件的扩展名
string extName = System.IO.Path.GetExtension(file.FileName);
if (extName != ".jpeg" && extName != ".gif" && extName != ".jpg" && extName != ".png")
{
context.Response.Write("文件格式不合法!");
return;
}
//最终上传路径
string uploadImgName = "/Upload/" + Guid.NewGuid().ToString() + file.FileName;
//将上传的图片保存
file.SaveAs(context.Request.MapPath(uploadImgName));
context.Response.Write("上传成功!");
}
public bool IsReusable
{
get
{
return false;
}
}
}
}