473,396 Members | 1,770 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

How to force download ?

Hi !
I have a problem with downloading csv files from a web app.
The files get created in the right directory, so I know they're there.
I get the error "The page cannot be found" when I click the download
link. Here's the code that does the work. (hopefully the format doesn't
get too screwed up here :-) The code looks pretty straight forward, but
I can't understand what's wrong with it. It should download the file,
if it's there...
Greatfull for any help !

Fredrik

--------------------------------------------------------------------------------------------------------------------------------

public static void WriteFile(byte[] fileArr, string fileName)
{
RemoveFiles();

string path = HttpContext.Current.Server.MapPath("/Reports/" +
fileName);

//Create a filestream to send to the user
FileStream fStream = new FileStream(path, FileMode.Create,
FileAccess.Write);

try
{
Log.Message(EventCategory.Web, "Writing to user : " + path);

fStream.Write(fileArr, 0, fileArr.Length);
}
catch (Exception ex)
{
CpLog.Exception(EventCategory.Web, ex);
}
finally
{
fStream.Close();
}
}

public static void UploadFile(string fileName)
{
try
{
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.AppendHeader("Content-Disposition",
"attachment; filename=" + fileName);
HttpContext.Current.Response.Cache.SetCacheability (HttpCacheability.Private);

string filePath =
ConfigurationSettings.AppSettings["ReportPath"].ToString();
Log.Message(EventCategory.Web, "Writing file to : " + filePath);
Log.Message(EventCategory.Web, "HttpHeader : " +
HttpContext.Current.Request.Headers.ToString());
HttpContext.Current.Response.Redirect(filePath + fileName);
}
catch(Exception ex)
{
Log.Exception(EventCategory.Web, ex);
}
finally
{
HttpContext.Current.Response.End();
}

}

May 23 '06 #1
2 1621

fr*******@gmail.com wrote:
Hi !
I have a problem with downloading csv files from a web app.
The files get created in the right directory, so I know they're there.
I get the error "The page cannot be found" when I click the download
link. Here's the code that does the work. (hopefully the format doesn't
get too screwed up here :-) The code looks pretty straight forward, but
I can't understand what's wrong with it. It should download the file,
if it's there...
Greatfull for any help !

Fredrik

--------------------------------------------------------------------------------------------------------------------------------

public static void WriteFile(byte[] fileArr, string fileName)


This newsgroup covers classic asp. DotNet is a different technology.
Try microsoft.public.dotnet.framework.aspnet.

--
Mike Brind

May 23 '06 #2
Ooops, sorry, my misstake. I've used this group alot before, but kind
of forgot that it's all about classic ASP.

fr*******@gmail.com skrev:
Hi !
I have a problem with downloading csv files from a web app.
The files get created in the right directory, so I know they're there.
I get the error "The page cannot be found" when I click the download
link. Here's the code that does the work. (hopefully the format doesn't
get too screwed up here :-) The code looks pretty straight forward, but
I can't understand what's wrong with it. It should download the file,
if it's there...
Greatfull for any help !

Fredrik

--------------------------------------------------------------------------------------------------------------------------------

public static void WriteFile(byte[] fileArr, string fileName)
{
RemoveFiles();

string path = HttpContext.Current.Server.MapPath("/Reports/" +
fileName);

//Create a filestream to send to the user
FileStream fStream = new FileStream(path, FileMode.Create,
FileAccess.Write);

try
{
Log.Message(EventCategory.Web, "Writing to user : " + path);

fStream.Write(fileArr, 0, fileArr.Length);
}
catch (Exception ex)
{
CpLog.Exception(EventCategory.Web, ex);
}
finally
{
fStream.Close();
}
}

public static void UploadFile(string fileName)
{
try
{
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.AppendHeader("Content-Disposition",
"attachment; filename=" + fileName);
HttpContext.Current.Response.Cache.SetCacheability (HttpCacheability.Private);

string filePath =
ConfigurationSettings.AppSettings["ReportPath"].ToString();
Log.Message(EventCategory.Web, "Writing file to : " + filePath);
Log.Message(EventCategory.Web, "HttpHeader : " +
HttpContext.Current.Request.Headers.ToString());
HttpContext.Current.Response.Redirect(filePath + fileName);
}
catch(Exception ex)
{
Log.Exception(EventCategory.Web, ex);
}
finally
{
HttpContext.Current.Response.End();
}

}


May 24 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Matthew Sims | last post by:
Is it possible to force a download without using the readfile function? My website setup consists of my server that serves the web pages plus a high-speed file server elsewhere on the internet...
2
by: Jonathan Driller | last post by:
I am attempting to block repeat downloads from a site. I need to have a landing page pass a referer to a secured page. I have tried a meta refresh redirect and several Javascript redirect...
0
by: Jake | last post by:
How can I force an image download with IE using ASP.net?? Here is the catch: the image I want to force a write with is located on another web server. I can only reference the image with a URL. ...
2
by: JP SIngh | last post by:
Hi All I am creating an asp application which is essentially a file management app. I am trying to get my head around a technically issue wonder if someone can offer some help. I want to be...
4
by: Kevin Muenzler, WB5RUE | last post by:
How do I force a browser to download a file instead of displaying it? In other words I have a page with MP3 and WMA files on it and I would like for the visitor to download the file instead of...
4
by: tma | last post by:
Short of zipping my file, are there any other alternatives to the below? I'm trying to add anchors in code and I need the link to force a download of the .pdf rather than open a new window or...
0
by: comp.lang.php | last post by:
I have a form that when you click the "Generate Report" submit button, it will force download a CSV file, required for this project. On the very same page you also have a "Search" submit button,...
2
by: comp.lang.php | last post by:
class ReportGenerator { function ReportGenerator() {} /** * Generate the HTTP headers necessary for this file type. Can be called statically *
3
by: Bouffa | last post by:
Hello everyone, I suppose you all know force-download scripts. The problem is that these scripts don't allow files to be splitted when downloading them via a download manager. I've found a...
0
by: jinnareddy | last post by:
Hi, I'm unable to download a file that is having a 2-byte char in its name (e.g.テ) using force download option. Though, am able to download file names involving ASCII chars. I have tried URL...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.