简单的上传图片文件
前台页面:
<!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("格式不符");
            }
}
}
 
                     
                    
                 
                    
                 
                
            
         
 
         浙公网安备 33010602011771号
浙公网安备 33010602011771号