Beispiel #1
0
        public async Task <ActionResult> Decompression([FromForm] IFormFile file)
        {
            try
            {
                string path         = _env.ContentRootPath;
                byte[] result       = null;
                string originalName = "";
                using (var memory = new MemoryStream())
                {
                    await file.CopyToAsync(memory);

                    byte[] bytes = memory.ToArray();
                    using (FileStream fStream = System.IO.File.Create(path + "/Copy2/" + file.FileName))
                    {
                        fStream.Write(memory.ToArray());
                    }
                    originalName = lzw.GetOriginalName(path + "/Copy2/" + file.FileName);
                    result       = lzw.Decompression(path + "/Copy2/" + file.FileName);
                }
                Archive response = new Archive
                {
                    Content     = result,
                    ContentType = "compressedFile / txt",
                    FileName    = originalName
                };
                return(File(response.Content, response.ContentType, response.FileName));
            }
            catch (System.Exception)
            {
                return(StatusCode(500));
            }
        }
        public async Task <ActionResult> DownloadFile(string id)
        {
            Archive archiveAux = new Archive();

            archiveAux._id = id;

            using (var client = new HttpClient())
            {
                client.BaseAddress = new Uri("http://localhost:62573/api/");
                client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));

                var postTask = client.PostAsJsonAsync <Archive>("downloadFile", archiveAux);
                postTask.Wait();

                var result = postTask.Result;

                if (result.IsSuccessStatusCode)
                {
                    string stringAux = await result.Content.ReadAsAsync <string>();

                    Archive allMessages = JsonSerializer.Deserialize <Archive>(stringAux);

                    #region "LZW Decompression"
                    LZW _lzw = new LZW();

                    using (FileStream fstream = System.IO.File.Create(_env.ContentRootPath + "/Copy2/" + allMessages.name))
                    {
                        fstream.Write(allMessages.message);
                        fstream.Close();
                    }
                    string originalName  = _lzw.GetOriginalName(_env.ContentRootPath + "/Copy2/" + allMessages.name);
                    byte[] resultArchive = _lzw.Decompression(_env.ContentRootPath + "/Copy2/" + allMessages.name);

                    System.IO.File.Delete(_env.ContentRootPath + "/Copy2/" + allMessages.name);
                    #endregion

                    return(File(resultArchive, "text/plain", originalName));
                }
                else
                {
                    return(StatusCode(500));
                }
            }
        }