private Resource_FilesStatus UploadFile(HttpContext context, HttpPostedFile Httpfiles)
        {
            Resource_FilesStatus Resource = new Resource_FilesStatus();

            if (Httpfiles != null && Httpfiles.ContentLength > 0)
            {
                //To verify that if the suffix name of the uploaded files meets the DNN HOST requirements
                Boolean retValue = FileSystemUtils.CheckValidFileName(Httpfiles.FileName);
                if (retValue)
                {
                    //构造文件的存放目录
                    String FileName       = Httpfiles.FileName;
                    String tempName       = WebHelper.GetStringParam(context.Request, "id", "");
                    String Extension      = Path.GetExtension(FileName);
                    String leftName       = FileName.Replace(Extension, "");
                    String FileWebPath    = String.Format("{0}PowerForms/Multiplefiles/{1}/{2}/{3}-[{4}]{5}", PortalSettings.HomeDirectory, ModuleId, DateTime.Now.ToString("yyyyMMdd"), leftName, tempName, Extension);
                    String FileServerPath = context.Request.MapPath(FileWebPath);

                    //构造文件对象
                    FileInfo Savefile = new FileInfo(FileServerPath);
                    //文件夹构造
                    if (!Savefile.Directory.Exists)
                    {
                        Savefile.Directory.Create();
                    }
                    //文件是否存在||存在的话需要重新构造名称filename_2.txt
                    Int32 FileIndex = 2;
                    while (Savefile.Exists)
                    {
                        FileWebPath    = String.Format("{0}PowerForms/Multiplefiles/{1}/{2}/{3}_{4}-[{5}]{6}", PortalSettings.HomeDirectory, ModuleId, DateTime.Now.ToString("yyyyMMdd"), leftName, FileIndex, tempName, Extension);
                        FileServerPath = context.Request.MapPath(FileWebPath);
                        Savefile       = new FileInfo(FileServerPath);
                        FileIndex++;
                    }
                    //构造需要返回的信息
                    Resource.name = FileName;
                    Resource.size = Httpfiles.ContentLength;
                    Resource.url  = FileWebPath;
                    Resource.type = Httpfiles.ContentType;


                    //构造指定存储路径
                    Httpfiles.SaveAs(Savefile.FullName);
                }
            }
            else
            {
                //文件大小为空或者是0
            }

            return(Resource);
        }
        // Upload entire file
        private void UploadWholeFile(HttpContext context, List <Resource_FilesStatus> statuses)
        {
            for (int i = 0; i < context.Request.Files.Count; i++)
            {
                var file = context.Request.Files[i];

                if (file != null && !String.IsNullOrEmpty(file.FileName) && file.ContentLength > 0)
                {
                    Resource_FilesStatus Resource = UploadFile(context, file);
                    statuses.Add(Resource);
                }
            }
        }
        // Upload partial file
        private void UploadPartialFile(string fileName, HttpContext context, List <Resource_FilesStatus> statuses)
        {
            if (context.Request.Files.Count != 1)
            {
                throw new HttpRequestValidationException("Attempt to upload chunked file containing more than one fragment per request");
            }
            var file = context.Request.Files[0];

            if (file != null && !String.IsNullOrEmpty(file.FileName) && file.ContentLength > 0)
            {
                Resource_FilesStatus Resource = UploadFile(context, file);
                statuses.Add(Resource);
            }
        }
Exemple #4
0
        /// <summary>
        /// 复制文件
        /// </summary>
        /// <param name="hpFile"></param>
        /// <param name="pmb"></param>
        /// <returns></returns>
        public static String CopyFile(Resource_FilesStatus Resource_File, PortalModuleBase pmb)
        {
            String   WebFilePath  = Resource_File.url;
            String   SaveFilePath = pmb.MapPath(WebFilePath);
            FileInfo SaveFile     = new FileInfo(SaveFilePath);



            String FileName = Resource_File.name.Replace(" ", "_");

            if (FileName.IndexOf(@"\") >= 0)
            {
                FileName = FileName.Substring(FileName.LastIndexOf(@"\"), FileName.Length - FileName.LastIndexOf(@"\")).Replace(@"\", "");
            }

            String Extension = Path.GetExtension(FileName).Replace(".", "");


            //构造保存路径
            String   FileUrl = FileName;
            FileInfo file    = new FileInfo(pmb.MapPath(String.Format("~/Portals/{0}/PowerForms/{1}/{2}", pmb.PortalId, pmb.ModuleId, FileName)));

            if (!file.Directory.Exists)
            {
                file.Directory.Create();
            }

            int ExistsCount = 1;

            //检测文件名是否存在
            while (file.Exists)
            {
                FileUrl = String.Format("{0}_{1}.{2}", FileName.Replace("." + Extension, ""), ExistsCount, Extension);
                file    = new FileInfo(pmb.MapPath(String.Format("~/Portals/{0}/PowerForms/{1}/{2}", pmb.PortalId, pmb.ModuleId, FileUrl)));
                ExistsCount++;
            }

            //保存文件到文件夹
            SaveFile.MoveTo(file.FullName);

            return(FileUrl);
        }