public ActionResult Download()
        {
            ClientFileInstallation file = new DownloadBuilder().GetFile();

            if (file != null)
            {
                byte[] fileData = file.Data;
                Response.AddHeader("Content-type", file.ContentType);
                Response.AddHeader("Content-disposition", "attachment; filename=" + file.FileName);
                byte[] dataBlock = new byte[0x1000];
                long   fileSize;
                int    bytesRead;
                long   totalBytesRead = 0;
                new DownloadBuilder().NewDownloadedFile(file.FileName);
                using (Stream st = new MemoryStream(fileData))
                {
                    fileSize = st.Length;
                    while (totalBytesRead < fileSize)
                    {
                        if (Response.IsClientConnected)
                        {
                            bytesRead = st.Read(dataBlock, 0, dataBlock.Length);
                            Response.OutputStream.Write(dataBlock, 0, bytesRead);
                            Response.Flush();
                            totalBytesRead += bytesRead;
                        }
                    }
                }
                Response.End();
            }
            return(RedirectToAction("Menu", "Dashboard"));
        }
        public void TestPathless()
        {
            // act
            Action act = () => DownloadBuilder.New().WithUrl(url).Build();

            // assert
            Assert.ThrowsException <ArgumentNullException>(act);
        }
        public void TestUrlless()
        {
            // act
            Action act = () => DownloadBuilder.New().WithFileLocation(path).Build();

            // assert
            Assert.ThrowsException <ArgumentNullException>(act);
        }
        public void TestSetName()
        {
            // act
            IDownload download = DownloadBuilder.New()
                                 .WithUrl(url)
                                 .WithFileLocation(path)
                                 .WithFileName(filename)
                                 .Build();

            // assert
            Assert.AreEqual(folder, download.Folder);
            Assert.AreEqual(filename, download.Filename);
        }
        public void TestSetFolderAndName()
        {
            // act
            IDownload download = DownloadBuilder.New()
                                 .WithUrl(url)
                                 .WithDirectory(folder)
                                 .WithFileName(filename)
                                 .Build();

            // assert
            Assert.AreEqual(folder, download.Folder);
            Assert.AreEqual(filename, download.Filename);
        }
Exemple #6
0
        public async Task Invoke(HttpContext context, FileExtensionContentTypeProvider _mimes)
        {
            IDownloadProvider downloadProvider = new DownloadBuilder(this.options).Build(context, _mimes);

            if (downloadProvider == null)
            {
                await this.next(context);

                return;
            }
            if (!context.Request.HasFormContentType && context.Request.Query.Count == 0)
            {
                context.Result404();
                return;
            }
            string filename = "";
            string path     = "";

            if (context.Request.Query["path"].Count > 0)
            {
                path = context.Request.Query["path"][0];
            }
            if (context.Request.Query["filename"].Count > 0)
            {
                filename = context.Request.Query["filename"][0];
            }

            if (context.Request.HasFormContentType && context.Request.Form["path"].Count > 0)
            {
                path = context.Request.Form["path"][0];
            }
            if (context.Request.HasFormContentType && context.Request.Form["filename"].Count > 0)
            {
                filename = context.Request.Form["filename"][0];
            }
            if (string.IsNullOrEmpty(path))
            {
                context.Result404();
                return;
            }
            if (string.IsNullOrEmpty(filename))
            {
                filename = System.IO.Path.GetFileName(path);
            }
            path     = System.Web.HttpUtility.UrlDecode(path);
            filename = System.Web.HttpUtility.UrlDecode(filename);
            downloadProvider.Download(this.hostingEnv, filename, path);
            return;
        }
        public void TestSetFolder()
        {
            // arrange
            var dir = Path.GetTempPath();

            // act
            IDownload download = DownloadBuilder.New()
                                 .WithUrl(url)
                                 .WithDirectory(dir)
                                 .Build();

            // assert
            Assert.AreEqual(dir, download.Folder);
            Assert.IsNull(download.Filename);
        }
        public void TestCorrect()
        {
            // act
            IDownload download = DownloadBuilder.New()
                                 .WithUrl(url)
                                 .WithFileLocation(path)
                                 .Configure(config => {
                config.ParallelDownload = true;
            })
                                 .Build();

            // assert
            Assert.AreEqual(folder, download.Folder);
            Assert.AreEqual(filename, download.Filename);
        }