-3

I want to download file from virtual path .

http://localhost:60181/DocTemplates/Forms/HO/test.pdf

File will be for dynamic extension means it may be pdf file or docs .

I want to download file using byte stream means first convert file into byte stream and after download it .

Above file download after clicking hyperlink .

I don't know how to do this task , please give me advice .

Thanks .

tereško
  • 58,060
  • 25
  • 98
  • 150
Ronak Patel
  • 630
  • 4
  • 15
  • 1
    What have you tried already to solve this problem? E.g. provide some code snippet – rbr94 Nov 22 '16 at 09:46
  • I suspect your actual question is the exact opposite of what you typed. You dont' want to download anything from a URL, you want to create a controller that sends files to clients when *they* send a request. You can return a FileResult with a simple `File(somePath)` call. You need to set your routing configuration so that anything after your controller is mapped to parameters. Eg, if your controller is `FormsController` you could create a route to map the rest of the URL to a parameter, or separate parts to separate parameters – Panagiotis Kanavos Nov 22 '16 at 09:47
  • There are a *lot* of questions on how to return files, and even more questions on how to set up routing. – Panagiotis Kanavos Nov 22 '16 at 09:48

2 Answers2

1

I used something simple like the Web Client in your controller.

System.Net.WebClient client = new WebClient();
client.DownloadFile("url", "directory + filename");

Just specify the url of the file you want to download and then the directory and file name you want to save the file as.

You can then do what you want with the file in your controller.

You can also try:

using (FileStream fs = File.OpenRead(path))
{
   int length = (int)fs.Length;
   byte[] buffer;

   using (BinaryReader br = new BinaryReader(fs))
   {
       buffer = br.ReadBytes(length);
   }

   Response.Clear();
   Response.Buffer = true;
   Response.AddHeader("content-disposition",        String.Format("attachment;filename={0}", Path.GetFileName(path)));
   Response.ContentType = "application/" +     Path.GetExtension(path).Substring(1);
   Response.BinaryWrite(buffer);
   Response.End();
}
1
using (var client = new WebClient())
{
    var content = client.DownloadData(url);
    using (var stream = new MemoryStream(content))
    {
        ...
    }
} 

maybe you're looking for this then

Pedro Luz
  • 973
  • 5
  • 14