views:

58

answers:

3

hello all, i'm trying to write code that download a file that located on the server. but the save as dialog wont open in IE.

i tried response.redirect, i tried

 Response.Clear();
 Response.ContentType = "text/csv"; 
 Response.AddHeader("Content-Disposition", "attachment; filename=" + fileNameDisplay); 
 Response.WriteFile(Server.MapPath(pathName + fileNameUnique)); 
 Response.Flush(); 
 Response.End(); 

every thing works on firefox and chrome , but not in internet explorer. i know that there is a security option for this in security ---> custom level ---> Downloads ---> automatic prompting for file downloads , that is always in disable mode and i need to switch it to enable in order it to work, but i don't wont that my users deal with this. how do i overcome this 'security problem' ?

is there any right way to deal with download files ? what is the right code to do that ?

thank you, gadym

A: 

You need explicitly tell that it is not opened content, add following headers:

Response.AppendHeader("Content-Disposition", "attachment; filename=" + fname);
Response.AppendHeader("Content-Transfer-Encoding", "binary");

Also it is strongly recommend to set explicit file length

Response.AppendHeader("Content-Length", responseContentLength.ToString());
Dewfy
A: 
var info = new FileInfo(path);

Response.Clear();

Response.AppendHeader("Content-Disposition", String.Concat("attachment; filename=", info.Name));
Response.AppendHeader("Content-Length", info.Length.ToString(System.Globalization.CultureInfo.InvariantCulture));
Response.AppendHeader("Content-Transfer-Encoding", "binary");
Response.ContentType = "text/csv";

Response.WriteFile(info.FullName, true);
Response.End();
abatishchev
thank you for your answers. but still it doesnt work. it doesnt give me any error . just wont open.is it becuase i need to add some definitions to the server?(mime types, folder options etc. )?please help.thanks, gadym.
gadym
@gadym: It's doubtful whether you need to configure additional MIME type. No error? No popup? Have you tried to run it in another environment where another security preferences?
abatishchev
yes. in my local machine it works fine. but when i upload the code to the server it's causes the problem. no popup and no error occurs. only if i add the link to trust sites it will work. but how do people download files from sites without dealing security prefenences?
gadym
@gadym: I'm afraid you need to configure IE security preferences some way to allow downloadable content, etc.
abatishchev
you say i need to configure the browser of the client by code? it's not legal, i think.
gadym
@gadym: Not by code but manually. Are your clients in accessible to you windows domain? Can you apply a policy? If clients are from Internet - this is their problem, you can't do anything, I guess.
abatishchev
gadym
@gadym: Is your testing/dev machine is in a domain with the server?
abatishchev
my dev machine is a local computer. after i finish buliding my site i'm uploading the code using ftp to my separate server.
gadym
@gadym: ok, I see. are you in domain with server? or this a separate, for example, hosting server?
abatishchev
it's domain with server
gadym
@gadym: Google or ask on ServerFault which policy control download politics
abatishchev
A: 

abatishchev, thank you for helping me. i found a soultion to my problem.

I created a dialog window (let's call it 'DownloadWindow') which holds an empty 'A HREF' tag. (it's shows - 'click here To Download')

after i click a download (EXCEL/CSV icon) button on my default page (which creates my file dynamicly), i load the dialog window ('DownloadWindow') and then i'm repleacing the 'a href' link to the file url i created earlier so my users can download it from my server.

Now, Internet explorer did pop up the open/save/cancel dialog box.

it's a bit annoying but it's solved my porblem.

gadym