Avatar of bigd2001grad
bigd2001grad
Flag for United States of America asked on

Add a linkbutton to download a file located on the server

I'm developing a web application written in C# and using .NET 1.1.

I want each user to download an excel file via a link button - such that when they click the linkbutton, a "save as" dialog appears so the user can save the file to his/her client machine.

I don't need anything fancy - the only other way I know to do this is very complicated (adding the file as a resource and extracting it before download).

Thank you in advance.
ASP.NET

Avatar of undefined
Last Comment
bigd2001grad

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
nauman_ahmed

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Jason Scolaro

bigd2001grad,

Potentially an even easier way might be to use the Response.WriteFile() method.  See this link:
http://aspalliance.com/articleViewer.aspx?aId=259&pId=-1

Good luck.
-- Jason
Sammy

This should do it

HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Charset ="";
HttpContext.Current.Response.ContentType ="application/msword";
string strFileName = "myfile.xls";
HttpContext.Current.Response.AddHeader("Content-Disposition",
  "inline;filename=" + strFileName);
 
HTH
bigd2001grad

ASKER
if the file is an excel file, would the content type be "application/msexcel"?
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
bigd2001grad

ASKER
I can get the "save as" dialog to appear and can save the file, but everytime I open the file - it simply contains data from my web page.

Does this mean the response can't find my file or am I missing something?
nauman_ahmed

For EXCEL you have to use application/vnd.ms-excel content type

Are you sure you are using Response.Clear(); before rendering the content?

--Nauman.
bigd2001grad

ASKER
Yeah, Response.Clear is there.

Could it be caused by a postback or some other event that populates the response object?
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
nauman_ahmed

What type of HTML output you have in the generateed XML file?

--Nauman.
bigd2001grad

ASKER
the excel file contains a bunch of information generated by my javascript menu and a few of the web controls form the home page.
Ashutosh Vyas

User Response.End at the end to avoid that
Your help has saved me hundreds of hours of internet surfing.
fblack61
nauman_ahmed

bigd2001grad,

This excel file is generated on runtime?

--Nauman.
bigd2001grad

ASKER
Nauman,

The file is not generated at runtime - it already exists in my solution
Ashutosh Vyas

did Response.End not help?
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
bigd2001grad

ASKER
no, Response.End didn't seem to do anything...
bigd2001grad

ASKER
Does this make sense to anyone?  Maybe I should try embedding my Excel file as a resource (making it a bit easier to deploy to different environments)...

string myDirectory = EnvironmentHandler.Instance.ActiveEnvironment.EnvironmentSettings.GetSettingValue("FileDirectory");
                  FileUtil.EnsureDirectoryExists(myDirectory);

                  // Get embedded resource stream.
                  Assembly assembly = Assembly.GetExecutingAssembly();
                  string[] assemblyNameParts = assembly.FullName.Split(',');
                  Stream resourceStream = assembly.GetManifestResourceStream("File.xls");

                  string fileName = "Generic_Template_Illinois.xls";
                  string fullFileName = FileUtil.GetFileFullPath(pricingRequestDir, fileName);

                  FileStream fileStream = new FileStream(fullFileName, FileMode.CreateNew);

                  const int size = 4096;
                  byte[] bytes = new byte[4096];
                  int numBytes;
                  while((numBytes = resourceStream.Read(bytes, 0, size)) > 0)
                  {
                        fileStream.Write(bytes, 0, numBytes);
                  }

                  resourceStream.Close();
                  fileStream.Close();
nauman_ahmed

Why cannot you put this file one folder below the root folder and use it when required?

--Nauman
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
bigd2001grad

ASKER
For a couple of reasons:
1) It's bad form to mix your reference files with with your .aspx files
2) Deployment is made more difficult - this solution will be re-used over and over in different environments, so I utiilize nant scripts to run deployment (just another one-off to worry about when moving solutions)
3) We have a reference folder for this kind of stuff, but it only contains assemblies and I'd hate to just throw it in there
bigd2001grad

ASKER
Thanks for all the help everyone - I've posted my results below:

// extract the file from the resource library
                  string genericTemplateDir = EnvironmentHandler.Instance.ActiveEnvironment.EnvironmentSettings.GetSettingValue("GenericTemplateDirectory");
                  FileUtil.EnsureDirectoryExists(genericTemplateDir);

                  // Get embedded resource stream.
                  // See this class library project for the Generic_Template_Illinois.xls resource
                  Assembly assembly = Assembly.Load("DirectEnergy.Cmt.Illinois.Adapter");
                  string[] assemblyNameParts = assembly.FullName.Split(',');
                  Stream resourceStream = assembly.GetManifestResourceStream(assemblyNameParts[0] + ".Generic_Template.xls");

                  string fileName = "Generic_Template.xls";
                  string fullFileName = FileUtil.GetFileFullPath(genericTemplateDir, fileName);

                  // Delete the file if it exists to ensure latest and greatest is available
                  if(File.Exists(fullFileName))
                  {
                        File.Delete(fullFileName);
                  }

                  FileStream fileStream = new FileStream(fullFileName, FileMode.Create);

                  const int size = 4096;
                  byte[] bytes = new byte[4096];
                  int numBytes;
                  while((numBytes = resourceStream.Read(bytes, 0, size)) > 0)
                  {
                        fileStream.Write(bytes, 0, numBytes);
                  }
                  
                  resourceStream.Close();
                  fileStream.Close();

                  Response.Clear();
                  Response.AddHeader("Content-Disposition","attachment; FileName=" + fileName.ToString());
                  Response.CacheControl = "private";
                  Response.ContentType = "application/vnd.ms-excel";
                  FileStream httpFileStream = File.Open(fullFileName,FileMode.Open);
                  byte[] byteArray = new byte[httpFileStream.Length];
                  httpFileStream.Read(byteArray,0,byteArray.Length);
                  httpFileStream.Close();
                  Response.BinaryWrite(byteArray);