Пример #1
0
        /// <summary>
        ///     上传附件
        /// </summary>
        /// <param name="postedFile">fileupload控件</param>
        /// <param name="msg">提示信息</param>
        /// <param name="guid">文件重命名guid</param>
        /// <param name="savePath">文件保存路径</param>
        /// <param name="fileLength">文件大小限制(4M:4194304)</param>
        /// <param name="filesType">文件类型限制,默认".gif,.jpg,.png,.xls,.xlsx,.doc,.docx,.pdf"</param>
        /// <returns></returns>
        public static FIReturnInfo UploadFile(HttpPostedFile postedFile, out string msg, out string guid,
            out string savePath,
            string filesType = FileType, int fileLength = FileBtyesLength)
        {
            msg = string.Empty;
            guid = Guid.NewGuid().ToString();
            savePath = string.Empty;
            string[] strArray = filesType.ToLower().Split(',');
            if (!string.IsNullOrEmpty(postedFile.FileName))
            {
                //文件类型,带".",如:.jpg,.doc
                string extension = Path.GetExtension(postedFile.FileName).ToLower();
                if (strArray.Length != 0)
                {
                    if (!Array.Exists(strArray, element => element.Equals(extension)))
                    {
                        string error = string.Join("/", strArray);
                        var returnInfo = new FIReturnInfo(false, "只允许上传" + error + "的文件");
                        return returnInfo;
                    }
                }
                if (postedFile.ContentLength > fileLength)
                {
                    //msg = Message.Common.SizeOverFlow + fileLength + "KB(" + fileLength/(1024*1024) + "M)";
                    var returnInfo = new FIReturnInfo(false, "上传文件大小过大,允许上传的文件最大为" + FileBtyesLength / (1024 * 1024) + "M");
                    return returnInfo;
                }

                savePath = savePath + "\\" + DateTime.Now.ToString("yyyyMM");
                if (!Directory.Exists(savePath))
                    Directory.CreateDirectory(savePath);
                savePath = string.Format("{0}\\{1}_{2}{3}", savePath,
                    Path.GetFileNameWithoutExtension(postedFile.FileName), guid, extension);
                //postedFile.SaveAs(savePath);
                FileStream stream = new FileStream(savePath, FileMode.Create);

                int length = 1024;
                byte[] b = new byte[length];
                try
                {
                    int size = postedFile.InputStream.Read(b, 0, length);

                    while (size > 0)
                    {
                        stream.Write(b, 0, size);
                        size = postedFile.InputStream.Read(b, 0, length);
                    }
                }
                finally
                {
                    if (stream != null)
                    {
                        stream.Flush();
                        stream.Close();
                    }
                }

                var fiReturnInfo = new FIReturnInfo(true);
                return fiReturnInfo;
            }
            return new FIReturnInfo(false, "没有选择上传文件");
        }
Пример #2
0
        /// <summary>
        ///     上传附件
        /// </summary>
        /// <param name="postedFile">fileupload控件</param>
        /// <param name="msg">提示信息</param>
        /// <param name="guid">文件重命名guid</param>
        /// <param name="savePath">文件保存路径</param>
        /// <param name="fileLength">文件大小限制(4M:4194304)</param>
        /// <param name="filesType">文件类型限制,默认".gif,.jpg,.png,.xls,.xlsx,.doc,.docx,.pdf"</param>
        /// <returns></returns>
        public static FIReturnInfo UploadFile(HttpPostedFile postedFile, out string msg, out string guid,
                                              out string savePath,
                                              string filesType = FileType, int fileLength = FileBtyesLength)
        {
            msg      = string.Empty;
            guid     = Guid.NewGuid().ToString();
            savePath = string.Empty;
            string[] strArray = filesType.ToLower().Split(',');
            if (!string.IsNullOrEmpty(postedFile.FileName))
            {
                //文件类型,带".",如:.jpg,.doc
                string extension = Path.GetExtension(postedFile.FileName).ToLower();
                if (strArray.Length != 0)
                {
                    if (!Array.Exists(strArray, element => element.Equals(extension)))
                    {
                        string error      = string.Join("/", strArray);
                        var    returnInfo = new FIReturnInfo(false, "只允许上传" + error + "的文件");
                        return(returnInfo);
                    }
                }
                if (postedFile.ContentLength > fileLength)
                {
                    //msg = Message.Common.SizeOverFlow + fileLength + "KB(" + fileLength/(1024*1024) + "M)";
                    var returnInfo = new FIReturnInfo(false, "上传文件大小过大,允许上传的文件最大为" + FileBtyesLength / (1024 * 1024) + "M");
                    return(returnInfo);
                }

                savePath = savePath + "\\" + DateTime.Now.ToString("yyyyMM");
                if (!Directory.Exists(savePath))
                {
                    Directory.CreateDirectory(savePath);
                }
                savePath = string.Format("{0}\\{1}_{2}{3}", savePath,
                                         Path.GetFileNameWithoutExtension(postedFile.FileName), guid, extension);
                //postedFile.SaveAs(savePath);
                FileStream stream = new FileStream(savePath, FileMode.Create);

                int    length = 1024;
                byte[] b      = new byte[length];
                try
                {
                    int size = postedFile.InputStream.Read(b, 0, length);

                    while (size > 0)
                    {
                        stream.Write(b, 0, size);
                        size = postedFile.InputStream.Read(b, 0, length);
                    }
                }
                finally
                {
                    if (stream != null)
                    {
                        stream.Flush();
                        stream.Close();
                    }
                }

                var fiReturnInfo = new FIReturnInfo(true);
                return(fiReturnInfo);
            }
            return(new FIReturnInfo(false, "没有选择上传文件"));
        }