该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
我的前台是html5中有个表单
后台是windows 2008 server IIS7.5的服务器
using System;
using System.Web;
using System.IO;
public class UploadFile : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
HttpPostedFile f1 = context.Request.Files["f1"];
String fileExt = System.IO.Path.GetExtension(f1.FileName);
System.Drawing.Image image = System.Drawing.Image.FromStream(f1.InputStream);
int newWidth = 300, newHeight = 200;
if ((decimal)image.Width / image.Height > (decimal)newWidth / newHeight)
{
newHeight = Convert.ToInt32((decimal)image.Height * newWidth / image.Width);
}
else if ((decimal)image.Width / image.Height < (decimal)newWidth / newHeight)
{
newWidth = Convert.ToInt32((decimal)image.Width * newHeight / image.Height);
}
System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(newWidth, newHeight);
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bmp);
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
System.Drawing.Rectangle rectDestination = new System.Drawing.Rectangle(0, 0, newWidth, newHeight);
g.DrawImage(image, rectDestination, 0, 0, image.Width, image.Height, System.Drawing.GraphicsUnit.Pixel);
bmp.Save(context.Server.MapPath("~/") + DateTime.Now.ToString("yyyyMMddHHmmss") + fileExt);
bmp.Dispose();
image.Dispose();
context.Response.Write("OK");
}
public bool IsReusable
{
get
{
return false;
}
}
}
提交表单后出现
/”应用程序中的服务器错误。未将对象引用设置到对象的实例。说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。
异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。
源错误:
行 11: context.Response.ContentType = "text/plain";
行 12: HttpPostedFile f1 = context.Request.Files["f1"];
行 13: String fileExt = System.IO.Path.GetExtension(f1.FileName);
行 14: System.Drawing.Image image = System.Drawing.Image.FromStream(f1.InputStream);
行 15: int newWidth = 300, newHeight = 200;
源文件: d:\wfxt\11\UploadFile.ashx 行: 13
堆栈跟踪:
[NullReferenceException: 未将对象引用设置到对象的实例。] UploadFile.ProcessRequest(HttpContext context) in d:\wfxt\11\UploadFile.ashx:13 System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +598 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +170
版本信息: Microsoft .NET Framework 版本:2.0.50727.4927; ASP.NET 版本:2.0.50727.4927