Pop up to save image in asp.net - asp.net

I am using asp.net 3.5 and C#.
I have a image which I want user can download.
Like, there would be a download button or link. When user click on this link he will be prompted with a pop up to save that image to his desktop.
I have tried with
<a href ="path" > </a>
but it is opening the image in other page, I want user to be prompted to either save or view the image,
please help
Thanks in advance

You need to write an IHttpHandler that serves the image along with a Content-Disposition header.
For example:
Response.AppendHeader("Content-Disposition", "attachment; filename=\"MyImage.png\"");
Response.TransmitFile(path);
You would probably pass the image name on the query-string.
If so, make sure it doesn't contain / or \, or attackers will be able to read arbitrary files.

You need to have another page or, better yet, an HttpHandler, that takes the image path as part of the query string or as a post parameter that will send the response with Content-Disposition set to attachment. Setting the content disposition this way will cause the browser to display the file download dialog. A slightly easier way, though it depends on the user doing something extra is simply to have the link open the image in a new page and let the user right-click on it and do a "Save As".
Download
or
<a href="/path/to/image" target="_blank">
Load Image in New Window then Use Save As</a>

Related

How to take image source from code behind after updating it from client side

I have the following scenario.
User is trying to update the profile image , I am using uploadify plugin to upload image to server , and in callback I am setting
<img src="" runat="server" class="user-image" id="uploadedImage" />
SRC attribute to the uploaded path. This part is working good , then when user clicks on Save button , i need somehow to get new picture path and save it to database.
user.ImageUrl = this.uploadedImage.Attributes["src"];
doen't brings a new picture path , and returns a previous path.
Can someone help pls ?
If you want to accomplish this you may use using hidden field, among other ways.
Please refer to: http://www.codeproject.com/Articles/603102/Persist-JavaScript-changes-on-postback,http://forums.asp.net/t/1428896.aspx/1,http://forums.asp.net/t/1285473.aspx.

Web form non-responsive after download

I'm working in ASP.NET (2.0) and we have a page where a user is able to select and download a series of files as a zip. I got this to work without undo difficulty by using the DotNetZip library (which is probably not relevant to the problem, but included for completeness.)
After the user checks which files they want to download, the page does a postback, and in the button click event handler, I use the following code:
Response.Clear();
Response.BufferOutput = false; // false = stream immediately
Response.ContentType = "application/zip";
Response.AddHeader("content-disposition", "filename=FileRequest.zip");
using (ZipFile zip = new ZipFile())
{
zip.AddFile(MapPath("/datafiles/fileToZip.pdf"), "");
zip.Save(Response.OutputStream);
}
Response.Close();
And this all seems to work great. The user clicks the button, a download window pops up, the user downloads the zip. All is good...
...until they decide they want to do something else on the page. The buttons in the form are no longer responsive. For instance, if I click the download button again, it does nothing. If I reload the page, I can repeat this behavior...it works once, then does nothing.
I'm not understanding why the browser doesn't send the new request. It's not "spinning" or otherwise acting busy. I thought that this might be a browser issue, but I've repeated it in both IE and Firefox, so it seems likely that it's something I'm not understanding. Strangely, it's only form submission elements that seem to be non-responsive. Javascript still works, and so do regular links.
So why is this happening, and how do I get around it?
The problem is likely down to you returning;
Response.ContentType = "application/zip";
Once this has been sent (along with the actual content), the browser would assume it has nothing left to do (I believe).
You probably should create a new window specifically for downloading the files by saving the file selection in a session parameter (or equivalent) and opening a popup window that has your download code in.
This will leave the parent page in a suitable state.
Content-disposition header seems to be a discouraged solution. But the same effect of ASP.NET forms not being responsive occurs if you use standard Redirect to a zip file.
I solved very similar problem (returning *.csv files by the server for download) using Tančev Saša's code in "Response.Redirect to new window" Q&A and it works great. Perhaps it might produce some popup warnings in some browsers, but I think this is how it's often done in download sites.

how to make a VisualBasic2010 web app open a new browser window for a .htm file

On my web app's home page, when the user clicks the "About" hyperlink control (System.Web.UI.WebControls.HyperLink in my default.aspx), I need another browser window to open containing an existing about.htm file.
There are other hyperlinks for "Purpose" and "Description" and "How to" and I would like each to open another browser, so that the user can refer to these while performing operations.
You should place the following code in page_l;oad event,
Hyperlink1.Attributes.Add("target","_blank")
or
you can directly change the target property of a hyperlink control to _blank
This will open the the link in new browser window.
System.Web.UI.WebControls.HyperLink have a Target property that needs to be set with _blank value as shown on example at http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.hyperlink.target.aspx
Hope this helps...

openning files in another browser page/tab

i have an action that return a file content. i added:
Response.AddHeader("Content-Disposition", "attactment; filename:\"" + survey.File + "\"");
so that the image would be opened in another tab/page, gets opened in the current tab/page.
whats wrong with the header?
The content-disposition header instructs the user agent how it should present the data, and it is usually used when serving up binary data (as opposed to plain text). When set to "attachment", the display of the content is contingent upon further action of the user. In other words, the user should receive a prompt and must decide what to do with the content (usually given an "Open" or "Save" option).
You can not programmatically force a hyperlink to open up in a new tab. Even if you could, you should not. This behavior should be controlled by the user agent. As a user, when I want to open something in a new tab, I use the mouse-wheel-click because that is how my browser is configured.
You cannot control browser's tab behaviour by using HTTP headers - there is nothing in your code that affects this.
What might help you is changing HTML code that points to your download - if you are using tag you can set its target attribute to _tab or _blank and it will work in many browsers.

Close pop up window after binary file is sent to browser

Is there a way to close a pop up window after the page writes binary data (PDF) to the browswer?
Here are the details:
Whenever my web appilcation needs to print, it will pass some parameters over to a pop up window, which will display the print options. When the user clicks on the Print button, it will set the src of a iframe that will call the page that does the printing.
I have PDFConverter to convert URL / HTML to a pdf file. At the end of the converting, it will write the binary to the browser. Here are some code:
response.AddHeader("Content-Type", "binary/octet-stream");
response.AddHeader("Content-Disposition",
"inline; filename=" + fileName + ".pdf; size=" + pdfBytes.Length.ToString());
response.Flush();
response.BinaryWrite(pdfBytes);
response.Flush();
After this is done, i will need to close the pop up window. However it seems like you can't do anything after the response is flushed. Any ideas?
Thanks in advance!
Angela
Instead of creating the iframe in the popup window, you could create it in the parent window. This way once the user clicks the print button, you could safely close the popup without interupting the printing process. But instead of going through all the pain of creating new popups windows which might be blocked by some browsers, I would simply create some placeholder in the main page so that the user could choose printing options and then print the document.
Just in case anyone else is having the same problem. This is the solution that seems to work for me.
I use the jQuery Simple Modal to show my option list page. On this page, I have a window timer running every 1 second to check against the server if the print job is done. I use ajax for that. Once the job is done, I update the session variable, and the ajax call to the server will pick up the session value and close the pop up window.

Resources