主题: 关于4.1版上传图片及显示图片的问题
作者: 枯树昏鸦, 发布日期: 2010-03-21 16:18:59, 浏览数: 8922
今天在“无惧”的指导下,终于把上传图片的问题给解决了,有几个地方需要注意:
1、修改image.html中89行代码为:var imageUploadJson = (typeof KE.g[id].imageUploadJson == 'undefined') ? '../../upload/imgUpload.ashx' : KE.g[id].imageUploadJson;   其中“../../upload/imgUpload.ashx”为你写的处理上传代码
2、然后在imgUpload.ashx中写2个方法,需要注意的是4.1版本是回传json字符串
private void returnFalse(string errMsg, HttpContext context)
{
        context.Response.AddHeader("Content-Type", "text/html; charset=UTF-8");
        context.Response.Write("{\"error\":1,\"message\":\"" + errMsg + "\"}");
}
private void returnTrue(string imgPath, HttpContext context)
{
        context.Response.AddHeader("Content-Type", "text/html; charset=UTF-8");
  注:以前一直显示不了的原因是少了上面的代码,加上就没问题了
        context.Response.Write("{\"error\":0,\"url\":\"" + imgPath + "\"}");
}
catch里调用returnFalse("插入图片失败,请重试!",context);
上传成功后调用returnTrue(path + "/" + fileName, context);  这里的path + "/" + fileName是我上传成功后的真实图片路径


作者: 夏日寒風, 发布日期: 2010-04-09 16:05:21

没看懂啊!你写这两个方法是什么什么意思?能不能把.ashx的全部代码发上来啊?

回复
作者: 枯树昏鸦, 发布日期: 2010-04-30 11:40:41

应上面仁兄的要求把代码贴出来

using System;
using System.Web;
using System.IO;

public class imgUpload : IHttpHandler {
   
    public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "text/plain";
        double deLength = double.Parse(System.Configuration.ConfigurationManager.AppSettings["defaultImageLength"]);//默认大小
        HttpPostedFile imgFile= context.Request.Files["imgFile"];
        if (imgFile.ContentLength > 0)
        {
            try
            {
                if (imgFile.ContentLength / 1024 > deLength)//单个文件不能大于设定大小
                {
                    returnFalse("上传的图片不能大于" + deLength + "k!", context);
                    return;
                }
                if (checkFile(imgFile) == false)
                {
                    returnFalse("图片格式仅支持jpg,gif,png,bmp的文件类型!", context);
                    return;
                }
                string path = "/images/editPic/" + DateTime.Now.ToString("yyyyMMdd");/*上传图片存放地址*/
                string _name = DateTime.Now.ToString("yyyyMMddhhmm") + System.Guid.NewGuid().ToString().Substring(0, 8);//图片名
                string fileName = _name + Path.GetExtension(imgFile.FileName);
                string saveName = context.Server.MapPath(path)+"/"+fileName;
                if (Directory.Exists(context.Server.MapPath(path)) == false)
                {
                    Directory.CreateDirectory(context.Server.MapPath(path));
                }
                imgFile.SaveAs( saveName );
                returnTrue(path + "/" + fileName, context);//上传成功后传回json字符串
            }
            catch(Exception ex)
            {                               
                returnFalse("插入图片失败,请重试!",context);
            }
        }
        else
        {
            returnFalse("图片不存在!",context);
        }
    }
         
    /// <summary>
    /// 上传失败调用
    /// </summary>
    /// <param name="errMsg"></param>
    /// <param name="context"></param>
    private void returnFalse(string errMsg, HttpContext context)
    {
        context.Response.AddHeader("Content-Type", "text/html; charset=UTF-8");
        context.Response.Write("{\"error\":1,\"message\":\"" + errMsg + "\"}");
    }

    /// <summary>
    /// 上传成功调用
    /// </summary>
    /// <param name="imgPath"></param>
    /// <param name="context"></param>
    private void returnTrue(string imgPath, HttpContext context)
    {
        context.Response.AddHeader("Content-Type", "text/html; charset=UTF-8");
        context.Response.Write("{\"error\":0,\"url\":\"" + imgPath + "\"}");
    }

    /// <summary>
    /// 检查上传文件是否合法
    /// </summary>
    /// <param name="controls"></param>
    /// <returns></returns>
    private bool checkFile(HttpPostedFile control)
    {
        string[] check = { "image/pjpeg", "image/jpeg", "image/gif", "image/png", "image/bmp" };
        foreach (string s in check)
        {
            if (control.ContentType.Equals(s))
            {
                return true;
            }
        }
        return false;
    }
}

回复
发表新帖 发表回复