For Programmers: Free Programming Magazines  


Home > Archive > ASP > May 2006 > How to force download ?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author How to force download ?
fredda054@gmail.com

2006-05-23, 7:56 am

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();
}

}

Mike Brind

2006-05-23, 6:56 pm


fredda054@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

fredda054@gmail.com

2006-05-24, 3:56 am

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



fredda054@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();
> }
>
> }


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com