日期:2014-05-17  浏览次数:20482 次

关于HttpPostedFile转换成byte[]的问题
我想用一个Flash插件来实现多图片的批量上传,但是插件本来的方法是把图片保存到文件夹,而我是想保存到数据库。所以我动手修改了一下其相应的ashx文件代码。以下是详细代码:

public class up_img : IHttpHandler
    {

        double deLength = 2048;//默认大小

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            System.Web.HttpPostedFile postFile = context.Request.Files[0];
            if (postFile != null)
            {
                if (IsAllowedExtension(postFile) == false)
                {
                    context.Response.Write("error:-5");//上传文件类型错误!
                    return;
                }
                if (postFile.ContentLength <= 0 || postFile.ContentLength / 1024 > deLength)
                {
                    //context.Response.Write((postFile.ContentLength / 1024).ToString());
                    context.Response.Write("error:-4");//Slip:上传文件大小错误!
                    return;
                }
                try
                {
                    Image drimg = Image.FromStream(postFile.InputStream);
                    int Height = drimg.Height;
                    int Width = drimg.Width;
                    byte[] bytes = PhotoManager.ConvertImage(drimg);
                    drimg.Dispose();
                    if (Height >= 300 && Width >= 300)
          &nbs