show "Save as" window - c#

How can i show a Save as window in asp.net and C#...
This is similar to the one that opens for uploading but that window is the open file...
I am not looking to save only one file...like
Response.AddHeader("Content-Disposition", "attachment; filename=" + myfile.Name);
can i make it ung ajax... please help... samples or suggestions..
thanks

As far as I know you don't have any way of interacting with the file system using javascript from inside the browser. I would imagine that this would be a fairly large security hole if you could. The best I can suggest is to package up your files in a zip file then send that in a response with the content disposition set to attachment, then let the browser handle it for you.

In your page:
<iframe src="getFile.aspx?id=1"/>
<iframe src="getFile.aspx?id=2/>
Then inside getFile.aspx
Response.AddHeader("Content-Disposition", "attachment;filename=" + filename);
Response.BinaryWrite(fileContents);
(Note: never tried this myself...)

Related

open dialog to download pdf

I have a gridview in which I have provided an option for the user to download the pdf files. When they click on the pdf icon sometimes it open the pdf file in a new tab and sometimes it starts downloading. How can i make it download always?
You need to add a button (image button, linknbutton or button) and handle the RowCommand event of GridView. In RowCommand handler you may write code to download a file.
You may use Response object's method.
string filepath=MapPath("~/files/file.pdf");
byte []bytes=System.IO.File.ReadAllBytes(filepath);
Response.Clear();
Response.ClearHeaders();
Response.AddHeader("Content-Type", "application/octet-stream");
Response.AddHeader("Content-Length", bytes.Length.ToString());
Response.AddHeader("Content-Disposition","attachment; filename=file.pdf");
Response.BinaryWrite(bytes);
Response.Flush();
Response.End();
In order to always force a download you need to add the Content-Disposition header as AVD showed; however, I find this totally unnecessary; I think it would suffice to have the link to the PDF open in a new window. In other words, have target="_blank" defined. Example:
invoice
Then, is up to the user whether he wants to save the file locally or just see it on the screen. I think the important thing is that this won't interfere with the current page the user is looking at.

open the save as dialogue box

I have a file that is in .pdf format for my website.
If you left click on the link, it will open the Adobe Reader and open the file.
What I want to do is when you left click, it opens a dialogue box that asks you where you want
to save the file.
I know you can do this by right clicking and choosing save as,
but is there any way by just
one simple left click to downlaod it?
Im sure there is some sort of hack to accomplish this but this is the feature that is installed with the browser when Adobe Reader is installed on the machine. Keeping this consistent for all users is the best approach. Do not bother with trying to make the left click open a save dialog. Users are accustomed to this behavior and will know how to save the PDF to thier local hard disk
Consistency is the key here.
two parts, you have to convert the pdf to a byte stream and add a content-disposition header to the html response.
first get the file and stream to a byte array
public byte[] GetDocument(string filePath)
{
return File.ReadAllBytes(filePath);
}
Then send it - as the very first thing in the Page_Load event - you'll need to pass the path to the file through session or query string to a new page, as you can't change the content type on an existing page you have already viewed in the browser.
Byte[] fileData = GetDocument(filePath);
Response.Clear();
Response.ClearHeaders();
Response.ClearContent();
Response.ContentType = "application/pdf";
// prompt to download
Response.AppendHeader("content-disposition", "attachment; filename=declarations.pdf");
Response.AppendHeader("content-length", fileData.Length.ToString());
Response.BinaryWrite(fileData);
Response.Flush();
Response.Close();
Add the pdf to a zip folder and add a link to the zip folder
Remove the pdf location from the href part of your link, and code an onClick method instead.
<a href="#" onClick="$:openSaveDialog">
<script>
function openSaveDialog() {
//Write a JavaScript function to prompt the user with a Save Dialog Box.
}
</script>

Download a file using AJAX

What's the best way to allow a user to pull down an RDP file, but to do so using AJAX? In other words, I have a hyperlink and I need an RDP file to be downloaded by the user, but without a full page refresh.
I tried to make an AJAX call using the following example for RDP. It seems to work on Chrome and Firefox, but not on any version of IE.
String content = <RDP Content Here>
Response.Clear();
Response.AddHeader("Content-Disposition", "attachment; filename=HelloWorld.rdp");
Response.Cache.SetCacheability(System.Web.HttpCacheability.NoCache);
Response.ContentType = "Content-Type=application/x-rdp rdp;charset=ISO-8859-1";
Response.AddHeader("Content-Length", content.Length.ToString());
Response.Write(content);
Response.End();
I'd create another page and put that code there. Then have that page open up in a new window when the link is clicked. Make you clear out any of the boiler plate code that VS automatically generates when you create a new file. That'll get you your data without a page refresh. I've done this with Excel pages and images and it's worked like a champ.

Problem with Save As dialog in IE 7,8 in ASP.NET

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
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();
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());
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.
I had the same problem when my link for file download was made using:
<a href="DownloadFile.aspx">
<input type="image" src="~/virtual/path/to/image.png" runat="server" />
</a>
The problem was gone when i changed it to:
<a href="DownloadFile.aspx">
<img src="~/virtual/path/to/image.png" runat="server" />
</a>

Link to retrieve pdf file from DB- in asp.net

I have retrieved the pdf file from Database and linked it to a "a href" tag to open pdf file using link. Now I want to give the open/save dialog box before a pdf file opened . - in javascript(by using onclick event in "a" tag to call the javascript)
You have to set the content-disposition header using C# to get this behavior in a browser.
Read this for more info
How do I prompt a "Save As" dialog for an accepted mime type?
Downloading a File with a Save As Dialog in ASP.NET
Example
Response.Clear();
Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName);
Response.AddHeader("Content-Length", targetFile.Length.ToString);
Response.ContentType = "application/pdf";
Response.WriteFile(targetFile.FullName);

Categories

Resources