Exemple #1
0
        /// <summary>
        /// 执行文件上传
        /// </summary>
        public override void Process()
        {
            byte[] uploadFileBytes;
            string uploadFileName;

            if (UploadConfig.Base64)
            {
                uploadFileName  = UploadConfig.Base64Filename;
                uploadFileBytes = Convert.FromBase64String(Request[UploadConfig.UploadFieldName]);
            }
            else
            {
                var file = Request.Files[UploadConfig.UploadFieldName];
                if (file == null)
                {
                    Result.State = UploadState.FileAccessError;
                    WriteJson(Result);
                    return;
                }
                uploadFileName = file.FileName;

                if (!UploadConfig.CheckFileType(uploadFileName))
                {
                    Result.State = UploadState.TypeNotAllow;
                    WriteJson(Result);
                    return;
                }
                if (!UploadConfig.CheckFileSize(file.ContentLength))
                {
                    Result.State = UploadState.SizeLimitExceed;
                    WriteJson(Result);
                    return;
                }

                uploadFileBytes = new byte[file.ContentLength];
                try
                {
                    file.InputStream.Read(uploadFileBytes, 0, file.ContentLength);
                }
                catch (Exception)
                {
                    Result.State = UploadState.NetworkError;
                    WriteJson(Result);
                    return;
                }
            }

            Result.OriginFileName = uploadFileName;

            var savePath  = PathFormatter.Format(uploadFileName, UploadConfig.PathFormat);
            var localPath = Server.MapPath(savePath);

            try
            {
                if (string.IsNullOrEmpty(localPath))
                {
                    Result.State = UploadState.Unknown;
                    return;
                }
                if (!Directory.Exists(Path.GetDirectoryName(localPath)))
                {
                    var diskPath = Path.GetDirectoryName(localPath);
                    if (string.IsNullOrEmpty(diskPath))
                    {
                        Result.State = UploadState.Unknown;
                        return;
                    }
                    Directory.CreateDirectory(diskPath);
                }
                File.WriteAllBytes(localPath, uploadFileBytes);
                Result.Url     = savePath;
                Result.FullUrl = UploadConfig.PreUrl + savePath;
                Result.State   = UploadState.Success;
            }
            catch (Exception e)
            {
                Result.State        = UploadState.FileAccessError;
                Result.ErrorMessage = e.Message;
            }
            finally
            {
                WriteJson(Result);
            }
        }
        /// <summary>
        /// 处理
        /// </summary>
        public override void Process()
        {
            var basePath     = AppDomain.CurrentDomain.BaseDirectory;
            var guIdFileName = Context.GetStringFromParameters("guId");
            var partIndex    = Context.GetIntFromParameters("partIndex");

            if (Context.Request.Files.Count < 1)
            {
                Result.State = UploadState.FileAccessError;
                WriteJson(Result);
            }
            HttpPostedFile postedFile = Context.Request.Files[0];

            if (string.IsNullOrEmpty(postedFile.FileName))
            {
                Result.State = UploadState.FileAccessError;
                WriteJson(Result);
            }
            var    filePostName   = postedFile.FileName;
            string partFileUpload = "/Uploadfile/PartFile";
            string path           = basePath + partFileUpload;

            if (!UploadConfig.CheckFileType(filePostName))
            {
                Result.State = UploadState.TypeNotAllow;
                WriteJson(Result);
            }

            if (!UploadConfig.CheckFileSize(postedFile.ContentLength))
            {
                Result.State = UploadState.SizeLimitExceed;
                WriteJson(Result);
            }
            string fileName = guIdFileName + ".part" + partIndex;
            string fpath    = path + "/" + fileName;

            //如果不存在该目录,则创建目录
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }

            Stream     reader     = postedFile.InputStream;
            var        buffersize = 1024;
            FileStream fStream    = new FileStream(fpath, FileMode.Create, FileAccess.Write, FileShare.Read, buffersize);

            byte[] buffer = new byte[buffersize];
            int    len    = reader.Read(buffer, 0, buffersize);

            while (len > 0)
            {
                fStream.Write(buffer, 0, len);
                len = reader.Read(buffer, 0, buffersize);
            }
            reader.Close();
            fStream.Close();
            string rmsg = partFileUpload + "/" + fileName;

            Result.State   = UploadState.Success;
            Result.Url     = rmsg;
            Result.FullUrl = UploadConfig.PreUrl + rmsg;
            WriteJson(Result);
        }