Exemplo n.º 1
0
        public ActionResult UploadPasteImg(HttpPostedFileBase imgfile)
        {
            string          url      = string.Empty;
            string          errorMsg = string.Empty;
            UploadViewModel um_mod   = new UploadViewModel();

            if (imgfile == null || imgfile.ContentLength == 0)
            {
                errorMsg = "情选择文件";
            }

            if (imgfile != null)
            {
                int userid = Convert.ToInt16(GetAccount().Id);
                //文件后缀
                string Suffixstr     = "." + imgfile.ContentType.Split('/')[1];
                string fileNameGuid  = Guid.NewGuid().ToString("N");
                string localFileName = "artf_" + userid + "_" + fileNameGuid + Suffixstr;
                string locAddr       = WebConfigurationManager.AppSettings["FileAddr"].ToString();//图片上传地址
                //相对路径
                string filePath = string.Format("/uploadFiles/{0}/", DateTime.Now.ToString("yyyyMM"));
                //文件需要存储的路径
                string localPath = string.IsNullOrEmpty(locAddr) ? System.Web.Hosting.HostingEnvironment.MapPath(filePath) : locAddr + filePath;
                if (!Directory.Exists(localPath))
                {
                    Directory.CreateDirectory(localPath);
                }
                //文件全路径
                string fullPath = localPath + localFileName;
                //guid重命名
                string fileName = System.Guid.NewGuid().ToString() + System.IO.Path.GetFileName(imgfile.FileName);
                //储存文件

                if (imgfile.ContentLength > 1024 * 500)
                {
                    ImagHelper.CompressImage(imgfile.InputStream, fullPath);
                }
                else
                {
                    imgfile.SaveAs(fullPath);
                }

                url = filePath + localFileName;
            }
            um_mod.Message  = errorMsg;
            um_mod.FilePath = url;

            return(Json(um_mod));
        }
Exemplo n.º 2
0
        public ActionResult UploadEditer(HttpPostedFileBase upload)
        {
            string url             = string.Empty;
            string errorMsg        = string.Empty;
            string CKEditorFuncNum = System.Web.HttpContext.Current.Request["CKEditorFuncNum"];

            if (upload == null || upload.ContentLength == 0)
            {
                errorMsg = "情选择文件";
            }
            else if (!IsAllowedType(AllowedSuffix.IMG, Path.GetExtension(upload.FileName)))
            {
                errorMsg = "只允许上传图片类型的文件";
            }
            else if (upload.ContentLength > 1024 * 1024 * 5)
            {
                errorMsg = "上传图片大小不可超过5M";
            }

            if (!string.IsNullOrEmpty(errorMsg))
            {
                return(Content("<script>window.parent.CKEDITOR.tools.callFunction(" + CKEditorFuncNum + ", \"" + url + "\",\"" + errorMsg + "\");</script>"));
            }

            if (upload != null)
            {
                int userid = Convert.ToInt16(GetAccount().Id);
                //文件后缀
                string Suffixstr     = Path.GetExtension(upload.FileName);
                string fileNameGuid  = Guid.NewGuid().ToString("N");
                string localFileName = "artf_" + userid + "_" + fileNameGuid + Suffixstr;
                string locAddr       = WebConfigurationManager.AppSettings["FileAddr"].ToString();//图片上传地址
                //相对路径
                string filePath = string.Format("/uploadFiles/{0}/", DateTime.Now.ToString("yyyyMM"));
                //文件需要存储的路径
                string localPath = string.IsNullOrEmpty(locAddr) ? System.Web.Hosting.HostingEnvironment.MapPath(filePath) : locAddr + filePath;
                if (!Directory.Exists(localPath))
                {
                    Directory.CreateDirectory(localPath);
                }
                //文件全路径
                string fullPath = localPath + localFileName;
                //guid重命名
                string fileName = System.Guid.NewGuid().ToString() + System.IO.Path.GetFileName(upload.FileName);
                //储存文件

                if (upload.ContentLength > 1024 * 500)
                {
                    ImagHelper.CompressImage(upload.InputStream, fullPath);
                }
                else
                {
                    upload.SaveAs(fullPath);
                }

                url = filePath + localFileName;
            }



            //上传成功后,我们还需要通过以下的一个脚本把图片返回到第一个tab选项
            return(Content("<script>window.parent.CKEDITOR.tools.callFunction(" + CKEditorFuncNum + ", \"" + url + "\");</script>"));
        }