Exemplo n.º 1
0
        /// <summary>
        /// 上传
        /// </summary>
        /// <returns>异步则返回进程ID,同步返回上传文件的路径</returns>
        public string Upload()
        {
            HttpRequest request = (HttpHosting.Context.RawContext() as HttpContext).Request;
            String      baseDir = EnvUtil.GetBaseDirectory();

            string[] process = request.Form["upload_process"][0].Split('|');
            string   processID = process[1], field = process[0];

            var postedFile = request.Form.Files[field];

            if (postedFile == null)
            {
                return(null);
            }
            string fileExt = postedFile.FileName.Substring(postedFile.
                                                           FileName.LastIndexOf('.') + 1); //扩展名

            InitUplDirectory(baseDir, this._saveAbsoluteDir);
            this._fileInfo = new UploadFileInfo
            {
                Id            = processID,
                ContentLength = postedFile.Length,
                FilePath      = $"{this._saveAbsoluteDir}{this._fileName}.{fileExt}"
            };
            String targetPath = baseDir + this._fileInfo.FilePath;

            if (!this._autoRename && File.Exists(targetPath))
            {
                throw new IOException("文件已存在");
            }
            // 自动将重复的名称命名
            int i = 0;

            while (File.Exists(targetPath))
            {
                i++;
                this._fileInfo.FilePath = $"{this._saveAbsoluteDir}{this._fileName}_{i.ToString()}.{fileExt}";
                targetPath = baseDir + this._fileInfo.FilePath;
            }
            this.SaveStream(postedFile.OpenReadStream(), targetPath);
            return(_fileInfo.FilePath);
        }
Exemplo n.º 2
0
        /// <summary>
        /// 上传
        /// </summary>
        /// <returns>异步则返回进程ID,同步返回上传文件的路径</returns>
        public string Upload(ICompatiblePostedFile postedFile)
        {
            ICompatibleRequest request = HttpHosting.Context.Request;
            String             baseDir = EnvUtil.GetBaseDirectory();

            string[] process   = request.Form("upload_process")[0].Split('|');
            string   processId = process[1];

            if (postedFile == null)
            {
                return(null);
            }

            string fileName = postedFile.GetFileName();
            string fileExt  = fileName.Substring(fileName.LastIndexOf('.') + 1); //扩展名

            InitUplDirectory(baseDir, this._saveAbsoluteDir);
            this._fileInfo = new UploadFileInfo
            {
                Id            = processId,
                ContentLength = postedFile.GetLength(),
                FilePath      = $"{this._saveAbsoluteDir}{this._fileName}.{fileExt}"
            };
            String targetPath = baseDir + this._fileInfo.FilePath;

            if (!this._autoRename && File.Exists(targetPath))
            {
                throw new IOException("文件已存在");
            }
            // 自动将重复的名称命名
            int i = 0;

            while (File.Exists(targetPath))
            {
                i++;
                this._fileInfo.FilePath = $"{this._saveAbsoluteDir}{this._fileName}_{i.ToString()}.{fileExt}";
                targetPath = baseDir + this._fileInfo.FilePath;
            }
            this.SaveStream(postedFile.OpenReadStream(), targetPath);
            return(_fileInfo.FilePath);
        }