C#写的照片上传代码--建立一个一般处理程序

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;


namespace PhotoUpload
{
    /// <summary>
    /// PhotoUpload 的摘要说明
    /// </summary>
    public class PhotoUpload : IHttpHandler
    {


        public void ProcessRequest(HttpContext context)
        {
            try
            {
                // Get the data
                HttpPostedFile jpeg_image_upload = context.Request.Files["Filedata"];


                // Retrieve the uploaded image
                var original_image = System.Drawing.Image.FromStream(jpeg_image_upload.InputStream);
                var extensionName = System.IO.Path.GetExtension(jpeg_image_upload.FileName);
                string remess = "";
                if (original_image.Width > 1200 || original_image.Height > 1200)
                {
                    remess += "error:图像宽度不能大于1200像素,高度不能大于1200像素。";
                }
                else
                {
                    string guid = Guid.NewGuid().ToString();
                    jpeg_image_upload.SaveAs(context.Request.MapPath("~") + "\\TempImages\\" + guid + extensionName); // 这个方法会读取项目中的WebConfig配置文件,然后拼接好照片上传的路径。我做的这个项目是放在服务器下的一个文件夹中;同时注意的是照片
                                                                                                                      //和Asp页面要分开放,所以用一个ashx程序单独做处理。减少服务器与客户端交互的压力。
                    remess += "ok:" + guid + extensionName;
                }
                context.Response.Write(remess);
            }
            catch
            {
                context.Response.Write("error:服务器错误,上传失败。");
            }
        }


        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值