/// <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);
        }
Exemplo n.º 2
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);
            }
        }
        public override void Process()
        {
            var guIdFileName = Context.GetStringFromParameters("guId");
            var partCount    = Context.GetIntFromParameters("partCount");
            var filePostName = Context.GetStringFromParameters("fileName");

            string partFileUpload = "/Uploadfile/PartFile";
            var    savePath       = PathFormatter.Format(filePostName, UploadConfig.PathFormat);
            var    localPath      = Server.MapPath(savePath);
            //分片文件列表
            var partFileList = new List <string>();

            if (!UploadConfig.CheckFileType(filePostName))
            {
                Result.State = UploadState.TypeNotAllow;
                WriteJson(Result);
            }
            if (string.IsNullOrEmpty(localPath))
            {
                Result.State = UploadState.Unknown;
                WriteJson(Result);
                return;
            }
            //如果不存在该目录,则创建目录
            if (!Directory.Exists(Path.GetDirectoryName(localPath)))
            {
                Result.State = UploadState.FileAccessError;
                WriteJson(Result);
            }
            for (int i = 1; i <= partCount; i++)
            {
                string fileName = guIdFileName + ".part" + i;
                string fpath    = partFileUpload + "/" + fileName;
                fpath = Server.MapPath(fpath);
                if (!File.Exists(fpath))
                {
                    Result.State = UploadState.FileAccessError;
                    WriteJson(Result);
                }
                partFileList.Add(fpath);
            }
            if (partFileList.Count < 1)
            {
                Result.State = UploadState.FileAccessError;
                WriteJson(Result);
            }


            var buffersize = 1024;
            //最终文件
            FileStream fStream = new FileStream(localPath, FileMode.Create, FileAccess.Write, FileShare.Read, buffersize);

            byte[] buffer = new byte[buffersize];

            partFileList.ForEach(f =>
            {
                // 要上传的文件
                FileStream fs    = new FileStream(f, FileMode.Open, FileAccess.Read);
                long totalLength = fs.Length;
                while (totalLength > 0)
                {
                    var length = fs.Read(buffer, 0, buffer.Length);
                    fStream.Write(buffer, 0, length);
                    totalLength = totalLength - length;
                }
                fs.Close();
            });
            fStream.Close();
            Result.State   = UploadState.Success;
            Result.Url     = savePath;
            Result.FullUrl = UploadConfig.PreUrl + savePath;
            if (partFileList.Count > 0)
            {
                try
                {
                    partFileList.ForEach(r =>
                    {
                        if (File.Exists(r))
                        {
                            File.Delete(r);
                        }
                    });
                }
                catch (Exception)
                {
                    // ignored
                }
            }
            WriteJson(Result);
        }