0

I have the following scenario in ASP.NET application: Create a file in a temporary directory and return a link to that file. I implement it as follows:

// get URL to create file
var tempDataOperationsDirectory = Server.MapPath("~/temp/DataOperations/foo1.txt");
// Create file
// Create link:
 ButtonViewFile.NavigateUrl = Page.ResolveUrl("~/temp/DataOperations/foo1.txt");

The above works. I am wondering if that's a good approach, and what if any alternatives are there?

laconicdev
  • 6,360
  • 11
  • 63
  • 89

2 Answers2

0

Why do you need to create that temporary file? If you to create a file dynamically just for a single download, you may want to consider implementing a HttpHandler class that can i.e. write a file stream directly to the response returned to the client's browser.

If you want more information about HttpHandler in ASP.NET, take a look here http://msdn.microsoft.com/en-us/library/ms228090.aspx or here Best way to stream files in ASP.NET.

Community
  • 1
  • 1
Lukasz M
  • 5,635
  • 2
  • 22
  • 29
  • This file is uploaded from a client, and we perform conversions, etc...We "create" it in order to allow the user to re-download the original file. – laconicdev Aug 13 '12 at 18:29
0

The above works. I am wondering if that's a good approach, and what if any alternatives are there?

It's a good approach and also the recommended one. It will always generate an accurate link. Other approaches I can think of may not be as accurate as this one.

Icarus
  • 63,293
  • 14
  • 100
  • 115