public string GenerateFile(ModelFile file)
        {
            try
            {
                int maxVal = int.Parse(file.MaxValue);
                Int64 _amount = Int64.Parse(file.FileAmount);

                using (BCRandomStream rndstream = new BCRandomStream(maxVal + 1))
                {
                    string fName = "GenerateFile" + file.Id + ".txt";
                    string path = Server.MapPath("~/" + fName);
                    using (FileStream fileStream = new FileStream(path, FileMode.Create, FileAccess.ReadWrite))
                    {
                        using (StreamWriter writeStream = new StreamWriter(fileStream))
                        {
                            for (var i = 0; i < _amount; i++)
                                writeStream.WriteLine(rndstream.Read());
                        }
                        return fName;
                    }
                }
            }
            catch
            {
                return null;
            }
        }
Beispiel #2
0
        public ActionResult Download(ModelFile file)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    GenerateFile(file.MaxValue, file.FileAmount);

                    var fileName = "GenerateFile.txt";

                    System.Web.HttpResponse response = System.Web.HttpContext.Current.Response;
                    response.ClearContent();
                    response.Clear();
                    response.ContentType = "text/plain";
                    response.AddHeader("Content-Disposition",
                                       "attachment; filename=" + fileName + ";");
                    response.TransmitFile(Server.MapPath("~/GenerateFile.txt"));
                    response.Flush();
                    response.End();

                }
            }
            catch
            {
                return View("Download");
            }

            return View();

            //return null;
        }
        public JsonResult RequestQueue(ModelFile file)
        {
            if (requestQueue == null)
            {
                requestQueue = new Queue<ModelFile>();
            }
            if (tasks == null)
            {
                tasks = new List<Task>();
            }
            if (downloadList == null)
            {
                downloadList = new List<string>();
            }
            requestQueue.Enqueue(file);
            var t = Task.Run(() =>
            {
                var ts = GenerateFile(file);
                downloadList.Add(ts);
            });
            tasks.Add(t);

            return Json(new { success = true }, JsonRequestBehavior.AllowGet);
        }