Example #1
0
        /// <summary>
        /// Convenient method to read the InputStream and return a byte array
        /// </summary>
        /// <returns>
        /// The byte array async.
        /// </returns>
        public async Task <byte[]> ReadAsBytesAsync()
        {
            MemoryStream stream;

            try {
                long length = InputStream.Length;
                stream = new MemoryStream((int)length);
            }
            catch (NotSupportedException) {
                stream = new MemoryStream();
            }
            await InputStream.CopyToAsync(stream);

            byte[] buffer;
            if (stream.GetBuffer().LongLength != stream.Length)
            {
                buffer = new byte[stream.Length];
                Array.Copy(stream.GetBuffer(), buffer, (int)stream.Length);
            }
            else
            {
                buffer = stream.GetBuffer();
            }
            return(buffer);
        }
Example #2
0
        public async Task <string> SaveAs(bool overwrite = false, bool autoCreateDirectory = true)
        {
            var rootDir     = UploadId.ToString("N");
            var destination = Path.Combine(_uploadDir, DateTime.Now.ToString("yyyyMM"),
                                           rootDir.Substring(00, 02), rootDir.Substring(02, 03),

                                           /** OPTIONAL
                                           ** keep the files send with same uploadId
                                           ** united in the same folder
                                           ** rootDir.Substring(05, 27),
                                           ***/UUI.ToString("N"));

            if (!JoinFiles)
            {
                var directory = new DirectoryInfo(destination);
                destination = Path.Combine(destination, PartIndex.ToString("0000000000"));
                if (autoCreateDirectory)
                {
                    directory.Create();
                }
                using (var file = new FileStream(destination, overwrite ? FileMode.OpenOrCreate : FileMode.CreateNew))
                {
                    await InputStream.CopyToAsync(file);
                }
                if (TotalParts == 0)
                {
                    var moveTo = Path.Combine(directory.FullName, Filename);
                    File.Move(destination, moveTo);
                    destination = moveTo;
                }
            }
            else
            {
                destination = await JoinFilesInOne(destination);
            }
            return(destination);
        }