关于.net上传图片不能预览的问题

来源:互联网 发布:软件数据储存在哪里 编辑:程序博客网 时间:2024/05/18 03:21

项目中的WebConfig页面必须要引用下面一段代码

<appSettings >

    <add key="uploadify_root" value="/upload/uploadify/"/>

</appSettings>

并且项目中要建立对应的文件夹来保存图片文件


下面是ashx文件

#region 上传图片
        /// <summary>
        /// 上传图片
        /// </summary>
        /// <param name="context"></param>
        private void UploadImage(HttpContext context)
        {
            string strRoot = System.Configuration.ConfigurationManager.AppSettings["uploadify_root"];
            StringBuilder sbMsg = new StringBuilder();
            String fileTypes = "gif,jpg,jpeg,png";
            int maxSize = 1024 * 1024 * 2;


            HttpPostedFile imgFile = context.Request.Files["Filedata"];
            if (imgFile == null)
            {
                sbMsg.Append("{\"code\":\"201\",\"tips\":\"请上传图片!\"}");
                context.Response.Write(sbMsg.ToString());
                context.Response.End();
            }


            String dirPath = context.Server.MapPath(strRoot);
            if (!Directory.Exists(dirPath))
            {
                try
                {
                    Directory.CreateDirectory(strRoot);
                }
                catch
                {
                    sbMsg.Append("{\"code\":\"201\",\"tips\":\"保存目录创建失败,请检查文件夹权限!\"}");
                    context.Response.Write(sbMsg.ToString());
                    context.Response.End();
                }
            }


            String fileName = imgFile.FileName;
            String fileExt = Path.GetExtension(fileName).ToLower();
            ArrayList fileTypeList = ArrayList.Adapter(fileTypes.Split(','));


            if (imgFile.InputStream.Length > maxSize)
            {
                sbMsg.Append("{\"code\":\"201\",\"tips\":\"超出最大上传限制(2M)!\"}");
                context.Response.Write(sbMsg.ToString());
                context.Response.End();
            }


            if (String.IsNullOrEmpty(fileExt) || Array.IndexOf(fileTypes.Split(','), fileExt.Substring(1).ToLower()) == -1)
            {
                sbMsg.Append("{\"code\":\"201\",\"tips\":\"请上传指定格式的图片文件!\"}");
                context.Response.Write(sbMsg.ToString());
                context.Response.End();
            }


            try
            {
                String ymd = DateTime.Now.ToString("yyyyMMdd", DateTimeFormatInfo.InvariantInfo);
                dirPath += ymd + "/";
                if (!Directory.Exists(dirPath))
                {
                    Directory.CreateDirectory(dirPath);
                }
                string newDate = DateTime.Now.ToString("yyyyMMddHHmmssffff", DateTimeFormatInfo.InvariantInfo);
                string strfn = StringUtility.FilterSpecial(imgFile.FileName);
                String newFileName = strfn.Remove(strfn.Length - fileExt.Length, fileExt.Length) + "_" + newDate + fileExt;


                //检测文件是否重名
                if (File.Exists(context.Server.MapPath(strRoot + ymd + "/" + newFileName)))
                {
                    sbMsg.Append("{\"code\":\"201\",\"tips\":\"已存在重名文件!\"}");
                    context.Response.Write(sbMsg.ToString());
                    context.Response.End();
                }
                String filePath = dirPath + newFileName;


                imgFile.SaveAs(filePath);


                String fileUrl = strRoot + ymd + "/" + newFileName;
                sbMsg.Append("{\"code\":\"200\",\"tips\":\"上传成功!\",\"url\":\"" + fileUrl.Replace("\\", "\\\\") + "\"}");
                context.Response.Write(sbMsg);
            }
            catch
            {
                context.Response.Write("{\"code\":\"201\",\"tips\":\"上传失败,请刷新后重试!\"}");
            }
        }
        #endregion


下面是ajax

//upload files
            $("#uploadify").uploadify({
                'uploader': 'ajax/fileUpload.ashx?action=upload_image',//指定一般处理程序 执行上传后的文件处理
                'fileSizeLimit': '2MB',
                'multi': false,
                'fileTypeDesc': '图片文件',
                'fileTypeExts': '*.gif; *.jpg; *.png',
                'onUploadSuccess': function (file, data, response) {
                    var msg = $.parseJSON(data);
                    if (msg.code == "200") {
                        $('#img_show').attr('src', msg.url);
                        $('#videocover').val(msg.url);
                    } else {
                        Public.showMessage(false, msg.tips);
                    }
                }
            });



注意ajax、获取的值 一定要在下面的id中获取

<td rowspan="6">图片预览
                   <div style="margin: 5px;" id="file_upload">
                        <input type="hidden" id="videocover" name="videocover" value="<%=strPicture %>" />
                        <img id="img_show" src="<%=strPicture %>" style="width: 185px; height: 150px; border: solid 1px #eeeeee;" />
                    </div>
                </td>




1 0
原创粉丝点击