Wednesday, 5 April 2017

Download file from an ASP.NET Web API method using C#

[HttpGet]
        [ResponseType(typeof(void))]
        public async Task<IHttpActionResult> DownloadFileAsync(inputmodel context)
        {
            HttpResponseMessage result = null;
             var documentDownload = await _documentManager.DownloadFileAsync(context);

//After getting byte array from below function we will pass to MemoryStream and this MemoryStream add to HttpResponseMessage

                if (documentDownload != null)
                {
                    var stream = new MemoryStream(documentDownload);
                    result = Request.CreateResponse(HttpStatusCode.OK);
                    result.Content = new StreamContent(stream);
                    result.Content.Headers.ContentType =
                        new MediaTypeHeaderValue("pdf");
                    result.Content.Headers.ContentDisposition =
                        new ContentDispositionHeaderValue("attachment")
                        {
                            FileName = string.Concat(FileName, ".", "pdf")
                        };
                }
                else
                {
                    result = Request.CreateResponse(HttpStatusCode.NoContent);
                }
            }
            return ResponseMessage(result);
        }


        Note => Below method is going to convert file into byte array    

public async Task<byte[]> DownloadFileAsync(ModelClass context)
        {
            string filepath = "Your Server path or local path" + @"\\" + context.Id + @"\\" + context.Guid + @"." + "pdf";
            var directorypath = "Your server path or local path" + @"\\" + context.Id;
            if (!Directory.Exists(directorypath)) return null;
            if (!File.Exists(filepath)) return null;
            var bytes = await Task.Run(() => File.ReadAllBytes(filepath));
            return bytes;
        }

Searching for a Specific Word in a Text File and Displaying in C#


      public static void Main()
        {
             Task.Run(async () => await Writefilecontent());
             Console.ReadLine();
         }


     private static async Task Writefilecontent()
        {
            string sourceFilePath = @"D:\Test\Source.txt";
            string destinationFilePath = @"D:\Test\Destination.txt";
            if (File.Exists(filepath))
            {
                var sb = new StringBuilder();
                var fs = new FileStream(sourceFilePath , FileMode.Open, FileAccess.Read);
                using (var sr = new StreamReader(fs))
                {
                    string line;
                    while ((line =await sr.ReadLineAsync()) != null)
                    {
                        if (line.Contains("this"))
                        {
                            sb.Append("this done !!!!!" + Environment.NewLine);
                        }
                    }
                }
                using (var sw = new StreamWriter(destinationFilePath , true)) // This is for writing text into some other file , you can do as your requirement. 
                {
                   await sw.WriteAsync(sb.ToString());
                }
                Console.WriteLine(sb.ToString());
                Console.ReadLine();
            }
        }

Note=> In StreamWriter we have to pass true other wise ,it will overwrite all text into  destination file.

like this

var sw = new StreamWriter(destinationFilePath , true)