473,406 Members | 2,633 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,406 software developers and data experts.

Downloading a file to client

Hi,

I have 2 problems when downloading a file from my ASP.net app to the client
PC. At the moment I'm doing it with the following code:

Response.ContentType = "application/octet-stream"

Response.AddHeader("Content-Disposition", "attachment; filename=" +
strFileToDownload)

Response.WriteFile(strFileToDownload)

The problems are:
1. How can I set the name of document to be downloaded? At the moment, the
name of file is by default set to be the name of the web form that it comes
from, which is far from ideal. to be able to call it, for example,
myFile.doc.
2. How can I try and force the save to be in a specific directory, or even
to default to the Windows temp directory, or to My Documents? At the moment,
the default target directory seems quite different on different machines.

All help very much appreciated.

Ian
ia*@xwebservices.net
Nov 17 '05 #1
2 4145
Also you might want to look at putting a marker such as " or ' around the
file name. If there are spaces in the filename, I don't know exactly that
would do, but I am sure it isn't going to be desired behavior.

Also it might be a setting on the broswer you are using. I had a web app
for the division I am in that worked fine, except when the site was used
outside the development group. The firm standard did not allow the filename
to be passed to the download dialog.

bill

"Peter O'Reilly" <Pe***********@timeinc.com!N!O!.S!P!AM!> wrote in message
news:eU**************@TK2MSFTNGP10.phx.gbl...
see in line comments...

--
Peter O'Reilly
"Ian Gordon" <xi*@dircon.co.uk> wrote in message
news:dy***************@news.dircon.co.uk...
Hi,

I have 2 problems when downloading a file from my ASP.net app to the client
PC. At the moment I'm doing it with the following code:

Response.ContentType = "application/octet-stream"

Response.AddHeader("Content-Disposition", "attachment; filename=" +
strFileToDownload)

Response.WriteFile(strFileToDownload)

The problems are:
1. How can I set the name of document to be downloaded? At the moment, the name of file is by default set to be the name of the web form that it

comes
from, which is far from ideal. to be able to call it, for example,
myFile.doc.


Code looks fine. Adding the Content-Disposition HTTP Response header is

the solution. It could very well be a bug with that browser version. Have you
tried requesting this file with a different browser?

Alternatively, if in your example, myFile.doc is truly a MS Word document,
have you tried changing the content type to more closely match the type of
file, i.e. application/ms-word? That may help.

2. How can I try and force the save to be in a specific directory, or even to default to the Windows temp directory, or to My Documents? At the moment,
the default target directory seems quite different on different machines.

Sorry, no dice. It's this way due to security considerations. One

solution would be to create your own (perhaps 'trusted') ActiveX downloader control, but that would be quite a bit more work to achieve such result.

Nov 17 '05 #2
Hi Ian,

The fix is in this line:
Response.AddHeader("Content-Disposition", "attachment; filename=" +
System.IO.Path.GetFileName(strFileToDownload))

The Content-Disposition header requires only the file name, not the entire
path. When given the entire path, it negates and the browser will not set a
name for the file download dialog.

Also, you should call Response.Clear() before you add your headers just to
make sure that there are no other headers being sent down to the browser.
As for the second part of your question, no. This is a serious breach of
security.

HTH,

Bill P.

On Wed, 30 Jul 2003 22:15:55 +0100, Ian Gordon <xi*@dircon.co.uk> wrote:
Hi,

I have 2 problems when downloading a file from my ASP.net app to the
client
PC. At the moment I'm doing it with the following code:

Response.ContentType = "application/octet-stream"

Response.AddHeader("Content-Disposition", "attachment; filename=" +
strFileToDownload)

Response.WriteFile(strFileToDownload)

The problems are:
1. How can I set the name of document to be downloaded? At the moment,
the
name of file is by default set to be the name of the web form that it
comes
from, which is far from ideal. to be able to call it, for example,
myFile.doc.
2. How can I try and force the save to be in a specific directory, or
even
to default to the Windows temp directory, or to My Documents? At the
moment,
the default target directory seems quite different on different machines.

All help very much appreciated.

Ian
ia*@xwebservices.net


--
Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
Nov 17 '05 #3

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

Similar topics

2
by: AlexB | last post by:
I'm trying to download a text file to an IE client using the following code. The problem is that the saved file also includes the html for the page. Any ideas anyone? Private Sub...
3
by: Rob Nicholson | last post by:
The following code added to Page_Load works a treat: Response.ContentType = "application/zip" Response.AppendHeader("Content-Disposition", "attachment; filename=DesktopChange.pdf")...
6
by: Shawn | last post by:
Hi. How can I download a file and store it on the web server. I have a complete URL to the file, but I never know what kind of file it is. It can be pdf, jpg, tif, doc, xls etc. Thanks, Shawn
1
by: just.starting | last post by:
Hi, My dot net client downloads files and checks for any new files time to time. The server is apache2.0.53 server. So what happens is that my file download thing works fine if I dont try to call...
3
by: just.starting | last post by:
Hi, My dot net client downloads files and checks for any new files time to time. The server is apache2.0.53 server. So what happens is that my file download thing works fine if I dont try to call...
5
by: roland.groenvynck | last post by:
I try to build my first web form application with C#. I want to program the downloading of a file to the client . Therefore I can use WebClient.DownloadFile (url, filepath ); I would like that...
35
by: keerthyragavendran | last post by:
hi i'm downloading a single file using multiple threads... how can i specify a particular range of bytes alone from a single large file... for example say if i need only bytes ranging from...
1
by: apondu | last post by:
Hi, I am new to web services and i am facing a problem. I am interested in downloading some file from internet and use it for furthur processing For eg. i have a file at a the following URL and...
2
Markus
by: Markus | last post by:
I want the download button to update while a file download is in progress - "Connecting", "Downloading..", etc. But with the code below, it does not update the text. I've read about...
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: 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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.