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;
}
}
}
}
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;
}
}
}
}