-2

I think this question is repeated in many places. But I would like to know the better solution which won't give overhead to server.

My scenario is like a user should be able to click on a link in website and that link will get the correct file from server and send it to user.

I see solutions like below,

string filename1 = "newfile.txt";
string filename = @"E:\myfolder\test.txt";
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", "attachment; filename=\"" + filename1 + "\"");
Response.TransmitFile(filename);

and like in below post,

Download/Stream file from URL - asp.net

Community
  • 1
  • 1
blue
  • 833
  • 2
  • 12
  • 39
  • What have you tried and how is it not working? What do you mean by "the better solution which won't give overhead to server"? If you link to a file, clicking that link will generally download that file. – David Dec 17 '14 at 16:02
  • MVC is part ASP.NET. Perhaps you mean you're using Web Forms instead of MVC. And it's hard to know what overhead you have if you're not showing us how you're obtaining the files and sending them to the client. – mason Dec 17 '14 at 16:03
  • I've edited the question, both the solutions are working in a dev server. But I'm wondering which one will perform better in production environment when multiple users download the files and the file size is large. – blue Dec 17 '14 at 16:19
  • @blue You'll need to do your own testing to determine which is more appropriate. – mason Dec 17 '14 at 20:31

1 Answers1

2

The differences I spotted between the 2 methods you mention is: 1. Uses Response.TransmitFile 2. Uses Response.WriteFile

In order to compare the two methods you should look at these links:

TransmitFile Writes the specified file directly to an HTTP response output stream, without buffering it in memory.

WriteFile Writes the specified file directly to an HTTP response output stream.

Clearly, Response.TransmitFile() sends the file to the client machine without loading it to the Application memory on the server, but Response.WriteFile() method loads the file that is being downloaded in the Application memory area of the server.

I would say use Response.TransmitFile() for larger files based on this alone.

However, you will need to look into how this impacts other parts of your application before you make a final decision.

This has been pretty comprehensively debated on various forums. Look for it.

GVashist
  • 427
  • 4
  • 16