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 JsonResult Upload(int Suffix, string guid, HttpPostedFileBase file, int chunk = 0, int chunks = 0, int userid = 0)
        {
            userid = Convert.ToInt16(GetAccount().Id);
            string locAddr = WebConfigurationManager.AppSettings["FileAddr"].ToString();//图片上传地址

            UploadViewModel um_mod = new UploadViewModel();

            if (file == null || file.ContentLength == 0)
            {
                um_mod.Message = "情选择文件";
            }
            else if (!IsAllowedType((AllowedSuffix)Suffix, Path.GetExtension(file.FileName)))
            {
                um_mod.Message = "该类型不允许上传";
            }
            else
            {
                //图片缩率图
                string thumbFileName = string.Empty;
                //文件后缀
                string Suffixstr = Path.GetExtension(file.FileName);
                um_mod.Extension = Suffixstr;
                string fileNameGuid = Guid.NewGuid().ToString("N");
                //生成新文件名称
                string localFileName = "art_" + userid + "_" + fileNameGuid + Suffixstr;

                if (IsAllowedType(AllowedSuffix.IMG, Suffixstr))
                {
                    thumbFileName = string.Format("small_art_{0}_{1}", userid, fileNameGuid + Suffixstr);
                }

                //相对路径
                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;

                #region 分片上传功能
                //临时文件路径
                string tempPath = localPath + "/temp_" + guid + "/";
                //如果文件分片,那么路径就需要转换为临时文件存储的路径
                if (chunks > 1)
                {
                    if (!Directory.Exists(tempPath))
                    {
                        Directory.CreateDirectory(tempPath);
                    }
                    fullPath = tempPath + GetFileTopStr(chunk) + "_" + file.FileName;
                }
                try
                {
                    um_mod.FilePath = filePath + localFileName;
                    //储存文件
                    file.SaveAs(fullPath);
                    //返回是否上传成功
                    um_mod.IsSuccess = true;
                }
                catch (Exception ex)
                {
                    um_mod.Message = ex.Message;
                }
                //判断是否切分过图片
                if (chunks > 1)
                {
                    // 获取该目录下所有的切片
                    string[] fileList = Directory.GetFileSystemEntries(tempPath);
                    //如果该目录下的文件数量=总片数量,那么就进行合成并且删除无用文件
                    if (fileList.Length == chunks)
                    {
                        try
                        {
                            um_mod.FilePath = filePath + localFileName;
                            //合并文件并生成新的文件
                            CombineFile(fileList, localPath + localFileName);
                            //删除临时文件
                            DeleteFolder(tempPath);

                            //返回是否上传成功
                            um_mod.IsSuccess = true;
                        }
                        catch (Exception ex)
                        {
                            um_mod.Message = ex.Message;
                        }
                    }
                }
                #endregion

                if (um_mod.IsSuccess)
                {
                    //返回源文件名称
                    um_mod.FileName = file.FileName;
                    //返回文件GUID名称
                    um_mod.FileGuidName = localFileName;

                    try
                    {
                        //生成图片缩略图
                        if (!string.IsNullOrEmpty(thumbFileName) && Suffix == (int)AllowedSuffix.IMG)
                        {
                            string thumbFullPath = localPath + thumbFileName;
                            ImagHelper.MakeThumbnail(fullPath, thumbFullPath, 150, 90, ThumbnailModel.Fit);
                        }
                    }
                    catch
                    {
                    }
                }
            }

            // 额外处理逻辑,后续逻辑有变动可删除
            //if (!string.IsNullOrEmpty(um_mod.FilePath))
            //{
            //    um_mod.FilePath = um_mod.FilePath.Replace("/uploadFiles", "");
            //}

            return(Json(um_mod));
        }
Exemplo n.º 3
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>"));
        }