示例#1
0
        public async Task should_extract_formdata_values()
        {
            var chunkFileName = _filepath + _filename;

            using (var stream = new FileStream(chunkFileName, FileMode.Open))
            {
                var multiPartContent = new MultipartFormDataContent("boundary=---skj2vj42al0dk45vda");
                var streamContent    = new StreamContent(stream);

                streamContent.Headers.ContentType        = new MediaTypeHeaderValue("multipart/form-data");
                streamContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("file")
                {
                    FileName = _filename
                };

                multiPartContent.Add(streamContent);
                multiPartContent.Add(new StringContent("1234"), "correlationId");
                multiPartContent.Add(new StringContent("1"), "chunkNumber");
                multiPartContent.Add(new StringContent("4"), "totalChunks");

                var request = new HttpRequestMessage
                {
                    Content = multiPartContent
                };

                await request.Content.ReadAsMultipartAsync(_provider);

                _provider.ExtractValues();

                _provider.ChunkFilename.Should().NotBeNullOrEmpty();
                _provider.CorrelationId.Should().NotBeNullOrEmpty();
                _provider.CorrelationId.ShouldBeEquivalentTo("1234");
                _provider.ChunkNumber.Should().NotBe(null);
                _provider.ChunkNumber.ShouldBeEquivalentTo("1");
                _provider.TotalChunks.Should().NotBe(null);
                _provider.TotalChunks.ShouldBeEquivalentTo("4");
            }
        }
示例#2
0
        public async Task <bool> UploadChunk(HttpRequestMessage request)
        {
            var provider = new CustomMultipartFormDataStreamProvider(_path);
            await request.Content.ReadAsMultipartAsync(provider);

            provider.ExtractValues();

            UploadSession uploadSession;

            _uploadSessions.TryGetValue(provider.CorrelationId, out uploadSession);

            if (uploadSession == null)
            {
                throw new ObjectNotFoundException();
            }

            var completed = uploadSession.AddChunk(provider.Filename, provider.ChunkFilename, provider.ChunkNumber, provider.TotalChunks);

            if (completed)
            {
                await uploadSession.MergeChunks(_path);

                var fileBlob = new FileBlob()
                {
                    Id   = Guid.NewGuid(),
                    Path = _path + uploadSession.Filename,
                    Name = uploadSession.Filename,
                    Size = uploadSession.Filesize
                };

                _db.FileBlobs.Add(fileBlob);
                await _db.SaveChangesAsync();

                return(true);
            }

            return(false);
        }