コード例 #1
0
        /// <summary>
        /// List导出到Excel文件
        /// </summary>
        /// <param name="sFileName"></param>
        /// <param name="sHeaderText"></param>
        /// <param name="list"></param>
        public string ExportToExcel(string sFileName, string sHeaderText, List <T> list, string[] columns)
        {
            sFileName = string.Format("{0}_{1}", SecurityHelper.GetGuid(), sFileName);
            string sRoot         = GlobalContext.HostingEnvironment.ContentRootPath;
            string partDirectory = string.Format("Resource{0}Export{0}Excel", Path.DirectorySeparatorChar);
            string sDirectory    = Path.Combine(sRoot, partDirectory);
            string sFilePath     = Path.Combine(sDirectory, sFileName);

            if (!Directory.Exists(sDirectory))
            {
                Directory.CreateDirectory(sDirectory);
            }
            using (MemoryStream ms = CreateExportMemoryStream(list, sHeaderText, columns))
            {
                using (FileStream fs = new FileStream(sFilePath, FileMode.Create, FileAccess.Write))
                {
                    byte[] data = ms.ToArray();
                    fs.Write(data, 0, data.Length);
                    fs.Flush();
                }
            }
            return(partDirectory + Path.DirectorySeparatorChar + sFileName);
        }
コード例 #2
0
        /// <summary>
        /// 上传单个文件
        /// </summary>
        /// <param name="fileModule"></param>
        /// <param name="fileCollection"></param>
        /// <returns></returns>
        public async static Task <TData <string> > UploadFile(int fileModule, IFormFileCollection files)
        {
            string         dirModule = string.Empty;
            TData <string> obj       = new TData <string>();

            if (files == null || files.Count == 0)
            {
                obj.Message = "请先选择文件!";
                return(obj);
            }
            if (files.Count > 1)
            {
                obj.Message = "一次只能上传一个文件!";
                return(obj);
            }
            TData     objCheck = null;
            IFormFile file     = files[0];

            switch (fileModule)
            {
            case (int)UploadFileType.Portrait:
                objCheck = CheckFileExtension(Path.GetExtension(file.FileName), ".jpg|.jpeg|.gif|.png");
                if (objCheck.Tag != 1)
                {
                    obj.Message = objCheck.Message;
                    return(obj);
                }
                dirModule = UploadFileType.Portrait.ToString();
                break;

            case (int)UploadFileType.News:
                if (file.Length > 5 * 1024 * 1024)     // 5MB
                {
                    obj.Message = "文件最大限制为 5MB";
                    return(obj);
                }
                objCheck = CheckFileExtension(Path.GetExtension(file.FileName), ".jpg|.jpeg|.gif|.png");
                if (objCheck.Tag != 1)
                {
                    obj.Message = objCheck.Message;
                    return(obj);
                }
                dirModule = UploadFileType.News.ToString();
                break;

            case (int)UploadFileType.Import:
                objCheck = CheckFileExtension(Path.GetExtension(file.FileName), ".xls|.xlsx");
                if (objCheck.Tag != 1)
                {
                    obj.Message = objCheck.Message;
                    return(obj);
                }
                dirModule = UploadFileType.Import.ToString();
                break;

            default:
                obj.Message = "请指定上传到的模块";
                return(obj);
            }
            string fileExtension = TextHelper.GetCustomValue(Path.GetExtension(file.FileName), ".png");

            string newFileName = SecurityHelper.GetGuid() + fileExtension;
            string dir         = "Resource" + Path.DirectorySeparatorChar + dirModule + Path.DirectorySeparatorChar + DateTime.Now.ToString("yyyy-MM-dd").Replace('-', Path.DirectorySeparatorChar) + Path.DirectorySeparatorChar;

            string absoluteDir      = Path.Combine(GlobalContext.HostingEnvironment.ContentRootPath, dir);
            string absoluteFileName = string.Empty;

            if (!Directory.Exists(absoluteDir))
            {
                Directory.CreateDirectory(absoluteDir);
            }
            absoluteFileName = absoluteDir + newFileName;
            try
            {
                using (FileStream fs = File.Create(absoluteFileName))
                {
                    await file.CopyToAsync(fs);

                    fs.Flush();
                }
                obj.Data        = Path.AltDirectorySeparatorChar + ConvertDirectoryToHttp(dir) + newFileName;
                obj.Message     = Path.GetFileNameWithoutExtension(TextHelper.GetCustomValue(file.FileName, newFileName));
                obj.Description = (file.Length / 1024).ToString(); // KB
                obj.Tag         = 1;
            }
            catch (Exception ex)
            {
                obj.Message = ex.Message;
            }
            return(obj);
        }