コード例 #1
0
ファイル: FilesController.cs プロジェクト: 1244952898/MQ_NEW
        //[HttpPost]
        public JsonResult Post(string id)
        {
            string userid   = CommonHelper.GetPostValue("key");
            string filename = CommonHelper.GetPostValue("fn");
            string type     = CommonHelper.GetPostValue("type").ToString("PublicFile");

            OrignalTypeEnum saveType = type.Equals("GuideFile") || type.Equals("3")
                ? OrignalTypeEnum.GuideFile
                : type.Equals("ActiveFile") || type.Equals("1")
                    ? OrignalTypeEnum.ActiveFile
                    : OrignalTypeEnum.PublicFile;

            //
            //这个方法暂时这样,需要优化
            //
            userid = LoginHelper.UserId.ToString();
            long lUserId = userid.ToLong(-1);

            //string.IsNullOrEmpty(sign) ||
            if (string.IsNullOrEmpty(userid) || lUserId <= 0)
            {
                return(Json(new FileUploadEntity {
                    ErrorCode = "10000", ErrorMessage = "非法请求"
                }));
            }
            T_BG_User bgUser = _bgUserService.GetUserById(userid.ToLong(-1));

            if (bgUser == null)
            {
                return(Json(new FileUploadEntity {
                    ErrorCode = "10000", ErrorMessage = "请重新登录"
                }));
            }
            if (HttpContext.Request.Files.Count > 0)
            {
                try
                {
                    HttpPostedFile file = System.Web.HttpContext.Current.Request.Files[0];
                    if (file.ContentLength <= 0)
                    {
                        return(Json(new FileUploadEntity {
                            ErrorCode = "10000", ErrorMessage = "上传的文档为空!"
                        }));
                    }

                    FileExtension[] fileExs = new FileExtension[] { FileExtension.DOC, FileExtension.DOCX, FileExtension.PDF };
                    string          fileExt = FileValidation.FileExtension(file, fileExs);
                    if (string.IsNullOrEmpty(fileExt))
                    {
                        return(Json(new FileUploadEntity {
                            ErrorCode = "10000", ErrorMessage = "请上传WORD、PDF格式的文档!"
                        }));
                    }

                    string originFileName = Regex.Replace(file.FileName, "." + fileExt, "." + fileExt, RegexOptions.IgnoreCase);
                    string savename       = Guid.NewGuid().ToString("N");
                    if (!string.IsNullOrEmpty(filename))
                    {
                        savename = filename.Replace(string.Format("{0}_", LoginHelper.UserId), "");
                    }
                    string ofile       = originFileName;
                    string newFileName = OriginalSaveHelper.CreateNewSaveFileName(userid, savename, fileExt);
                    string savePath    = OriginalSaveHelper.GetSavePath(newFileName, fileExt, type);
                    string fn          = newFileName.Replace(string.Format(".{0}", fileExt), "");
                    if (!Directory.Exists(savePath))
                    {
                        Directory.CreateDirectory(savePath);
                    }
                    string strMs = string.Empty;
                    //string exception = "服务端异常";
                    string newFilePath = string.Format(@"{0}\{1}", savePath, newFileName);
                    file.SaveAs(newFilePath);
                    int iPageNum = 0;
                    fileExt = fileExt.ToLower();
                    if (fileExt == "pdf" || fileExt.EndsWith("pdf"))
                    {
                        iPageNum = PdfSaveHelper.GetPdfFilePages(newFilePath, out strMs);
                    }
                    //如果iPageNum 不更新或不上传
                    if (iPageNum < 0)
                    {
                        return(Json(new FileUploadEntity {
                            ErrorCode = "10000", ErrorMessage = "服务器不能解析上传的pdf,请选择其他文件重新上传!"
                        }));
                    }

                    T_BG_UpFiles bgUpFiles = new T_BG_UpFiles {
                        filename = fn, filehash = FileHelper.GetFileHash(newFilePath), userid = lUserId, fileoriginname = originFileName, filepath = newFilePath, ext = fileExt, addtime = DateTime.Now, type = saveType.ToInt(0)
                    };

                    string fileorigin = originFileName.ReplaceSqlTag();
                    long   cnt        = _bgUpFilesService.GetListByUserIdAndFileNameAndExt(originFileName, lUserId, fileExt, saveType.ToInt(0));
                    long   reslt      = _bgUpFilesService.Add(bgUpFiles);
                    if (reslt > 0)
                    {
                        string oname  = fileorigin.ToLower().Replace("." + bgUpFiles.ext.ToLower(), "");
                        string attach = cnt <= 0
                            ? string.Format("{0}.{1}", oname, bgUpFiles.ext)
                            : string.Format("{0}({1}).{2}", oname, cnt, bgUpFiles.ext);

                        return(Json(new FileUploadEntity {
                            ErrorCode = "00000", ErrorMessage = fn, Attach = attach, FilePath = bgUpFiles.filepath, FileType = fileExt
                        }));
                    }
                    else
                    {
                        return(Json(new FileUploadEntity {
                            ErrorCode = "10003", ErrorMessage = "服务端保存文件失败"
                        }));
                    }
                }
                catch (Exception)
                {
                    return(Json(new FileUploadEntity {
                        ErrorCode = "10000", ErrorMessage = "服务端异常"
                    }));
                }
            }
            else
            {
                return(Json(new FileUploadEntity {
                    ErrorCode = "10002", ErrorMessage = "请选择文件"
                }));
            }
        }
コード例 #2
0
        // GET: EmailUppload
        public JsonResult SendFile()
        {
            string userid = CommonHelper.GetPostValue("key");
            string type   = "EmailFile"; //CommonHelper.GetPostValue("type").ToString("PublicFile");

            OrignalTypeEnum saveType = OrignalTypeEnum.EmailFile;

            userid = LoginHelper.UserId.ToString();
            long lUserId = userid.ToLong(-1);

            if (string.IsNullOrEmpty(userid) || lUserId <= 0)
            {
                return(Json(new FileUploadEntity {
                    ErrorCode = "10000", ErrorMessage = "非法请求!"
                }));
            }
            T_BG_User bgUser = _bgUserService.GetUserById(userid.ToLong(-1));

            if (bgUser == null)
            {
                return(Json(new FileUploadEntity {
                    ErrorCode = "10000", ErrorMessage = "请重新登录!"
                }));
            }

            if (HttpContext.Request.Files.Count > 0)
            {
                try
                {
                    HttpPostedFile file = System.Web.HttpContext.Current.Request.Files[0];
                    if (file.ContentLength <= 0)
                    {
                        return(Json(new FileUploadEntity {
                            ErrorCode = "10000", ErrorMessage = "上传的附件为空!"
                        }));
                    }
                    //FileExtension[] fileExs = new FileExtension[] { FileExtension.DOC, FileExtension.DOCX, FileExtension.PDF };
                    //string fileExt = FileValidation.FileExtension(file, fileExs);
                    //if (string.IsNullOrEmpty(fileExt))
                    //{
                    //    return Json(new FileUploadEntity { ErrorCode = "10000", ErrorMessage = "请上传WORD、PDF格式的文档!" });
                    //}

                    string fileExt        = Path.GetExtension(file.FileName);
                    string originFileName = Regex.Replace(file.FileName, "." + fileExt, "." + fileExt, RegexOptions.IgnoreCase);
                    string savePath       = OriginalSaveHelper.GetEmailSavePath(fileExt.Replace(".", ""), type);
                    if (string.IsNullOrEmpty(savePath))
                    {
                        return(Json(new FileUploadEntity {
                            ErrorCode = "10000", ErrorMessage = "上传的路径获取失败!"
                        }));
                    }
                    if (!Directory.Exists(savePath))
                    {
                        Directory.CreateDirectory(savePath);
                    }
                    string newFilePath = string.Format(@"{0}\{1}", savePath, file.FileName);
                    file.SaveAs(newFilePath);

                    T_BG_UpFiles bgUpFiles = new T_BG_UpFiles {
                        filename = file.FileName, filehash = FileHelper.GetFileHash(newFilePath), userid = lUserId, fileoriginname = originFileName, filepath = newFilePath, ext = fileExt, addtime = DateTime.Now, type = saveType.ToInt(0)
                    };
                    //long cnt = _bgUpFilesService.GetListByUserIdAndFileNameAndExt(originFileName, lUserId, fileExt, saveType.ToInt(0));
                    long reslt = _bgUpFilesService.Add(bgUpFiles);
                    if (reslt > 0)
                    {
                        return(Json(new FileUploadEntity {
                            ErrorCode = "00000", ErrorMessage = "上传成功", Attach = file.FileName, FilePath = bgUpFiles.filepath, FileType = fileExt
                        }));
                    }
                    else
                    {
                        return(Json(new FileUploadEntity {
                            ErrorCode = "10003", ErrorMessage = "服务端保存文件失败"
                        }));
                    }
                }
                catch (Exception)
                {
                    return(Json(new FileUploadEntity {
                        ErrorCode = "10000", ErrorMessage = "服务端异常"
                    }));
                }
            }
            else
            {
                return(Json(new FileUploadEntity {
                    ErrorCode = "10002", ErrorMessage = "请选择附件!"
                }));
            }
        }