コード例 #1
0
        private FlowJsPostChunkResponse PostChunkBase(HttpRequest request, string folder, FlowValidationRules validationRules)
        {
            var chunk         = new FlowChunk();
            var requestIsSane = chunk.ParseForm(request.Form);

            if (!requestIsSane)
            {
                var errResponse = new FlowJsPostChunkResponse();
                errResponse.Status = PostChunkStatus.Error;
                errResponse.ErrorMessages.Add("damaged");
            }

            List <string> errorMessages = null;
            var           file          = request.Files[0];

            var response = new FlowJsPostChunkResponse {
                FileName = chunk.FileName, Size = chunk.TotalSize, Identifier = chunk.Identifier
            };

            var chunkIsValid = true;

            if (validationRules != null)
            {
                chunkIsValid = chunk.ValidateBusinessRules(validationRules, out errorMessages);
            }

            if (!chunkIsValid)
            {
                response.Status        = PostChunkStatus.Error;
                response.ErrorMessages = errorMessages;
                return(response);
            }

            var chunkFullPathName = GetChunkFilename(chunk.Number, chunk.Identifier, folder);

            try
            {
                // create folder if it does not exist
                if (!Directory.Exists(folder))
                {
                    Directory.CreateDirectory(folder);
                }
                // save file
                file.SaveAs(chunkFullPathName);
            }
            catch (Exception)
            {
                throw;
            }

            // see if we have more chunks to upload. If so, return here
            for (int i = 1, l = chunk.TotalChunks; i <= l; i++)
            {
                var chunkNameToTest = GetChunkFilename(i, chunk.Identifier, folder);
                var exists          = File.Exists(chunkNameToTest);
                if (!exists)
                {
                    response.Status = PostChunkStatus.PartlyDone;
                    return(response);
                }
            }

            // if we are here, all chunks are uploaded
            var fileAry = new List <string>();

            for (int i = 1, l = chunk.TotalChunks; i <= l; i++)
            {
                fileAry.Add("flow-" + chunk.Identifier + "." + i);
            }

            MultipleFilesToSingleFile(folder, fileAry, chunk.FileName);

            for (int i = 0, l = fileAry.Count; i < l; i++)
            {
                try
                {
                    File.Delete(Path.Combine(folder, fileAry[i]));
                }
                catch (Exception)
                {
                }
            }

            response.Status = PostChunkStatus.Done;
            return(response);
        }
コード例 #2
0
ファイル: FlowJsRepo.cs プロジェクト: alphaeleven/FlowJs-MVC
        private FlowJsPostChunkResponse PostChunkBase(HttpRequest request, string folder, FlowValidationRules validationRules)
        {
            var chunk = new FlowChunk();
            var requestIsSane = chunk.ParseForm(request.Form);
            if (!requestIsSane)
            {
                var errResponse = new FlowJsPostChunkResponse();
                errResponse.Status = PostChunkStatus.Error;
                errResponse.ErrorMessages.Add("damaged");
            }

            List<string> errorMessages = null;
            var file = request.Files[0];

            var response = new FlowJsPostChunkResponse {FileName = chunk.FileName, Size = chunk.TotalSize};

            var chunkIsValid = true;
            if(validationRules != null)
                chunkIsValid = chunk.ValidateBusinessRules(validationRules, out errorMessages);

            if (!chunkIsValid)
            {
                response.Status = PostChunkStatus.Error;
                response.ErrorMessages = errorMessages;
                return response;
            }

            var chunkFullPathName = GetChunkFilename(chunk.Number, chunk.Identifier, folder);
            try
            {
                // create folder if it does not exist
                if (!Directory.Exists(folder)) Directory.CreateDirectory(folder);
                // save file
                file.SaveAs(chunkFullPathName);
            }
            catch (Exception)
            {
                throw;
            }

            // see if we have more chunks to upload. If so, return here
            for (int i = 1, l = chunk.TotalChunks; i <= l; i++)
            {
                var chunkNameToTest = GetChunkFilename(i, chunk.Identifier, folder);
                var exists = File.Exists(chunkNameToTest);
                if (!exists)
                {
                    response.Status = PostChunkStatus.PartlyDone;
                    return response;
                }
            }

            // if we are here, all chunks are uploaded
            var fileAry = new List<string>();
            for (int i = 1, l = chunk.TotalChunks; i <= l; i++)
            {
                fileAry.Add("flow-" + chunk.Identifier + "." + i);
            }

            MultipleFilesToSingleFile(folder, fileAry, chunk.FileName);

            for (int i = 0, l = fileAry.Count; i < l; i++)
            {
                try
                {
                    File.Delete(Path.Combine(folder, fileAry[i]));
                }
                catch (Exception)
                {
                }
            }

            response.Status = PostChunkStatus.Done;
            return response;
        }