コード例 #1
0
        public BaseResponse <List <BaseFile> > UploadFile(UploadDirType path = UploadDirType.Doc)
        {
            List <BaseFile> files = new List <BaseFile>();

            string savePath = string.Format("/UploadImg/{0}/{1}", path.ToString(), DateTime.Now.ToString("yyyyMMdd"));

            DirectoryInfo dirinfo = new DirectoryInfo(HttpContext.Current.Server.MapPath(savePath));

            if (!dirinfo.Exists)
            {
                dirinfo.Create();
            }

            HttpFileCollection Files = HttpContext.Current.Request.Files;

            if (Files.Count > 0)
            {
                for (int i = 0; i < Files.Count; i++)
                {
                    HttpPostedFile file = Files[i];

                    string fileName, fileExtension;
                    //取得上传得文件名
                    fileName = Path.GetFileName(file.FileName);
                    //取得文件的扩展名
                    fileExtension = Path.GetExtension(fileName).ToLower();
                    string newFileName = Guid.NewGuid().ToString().Replace("-", "").ToLower();
                    string newFilePath = string.Format("{0}/{1}{2}", savePath, newFileName, fileExtension);
                    file.SaveAs(HttpContext.Current.Server.MapPath(newFilePath));
                    files.Add(BaseFile.CreateBaseFile(newFilePath));
                }
                return(BaseResponse.getResult(files, "上传成功"));
            }
            else
            {
                throw new ValiDataException("没有上传任何文件");
            }
        }