Downloading file using webclient with a save dialogue box - c#

I have one doubt, in my website i have to give an option for the front end users to download an exe from my website with a progress bar.
So can i use webClient.DownloadFile()?
But when i use
webClient.DownloadFileAsync(new Uri("http://xyztest.com/xyztest.exe"), "xyztest.exe");
then the exe is saving to my website's folder path.
I want to save the file with a save dialogue box and with a progress bar. Please give your suggestions.
Thanks,
Mahesh

You need to set the Content-Disposition: Attachment; of the xyztest.exe.
Then just specify the url in the browser after the user accepts and it will download automatically.

Related

Force the browser to save downloaded file in a specific location

My project is an Asp.Net MVC4 web application.
Currently it has a method to generate a text file and send it to the client's browser for download.
I need to modify it to force the browser to save the file in a custom (pre-defined) location on the client's computer.
This will not be possible as it would introduce a severe security problem. A user has to decide where the file will be saved.
You can only specify a location on a server to which you have access to.
If its an internal site, then you could setup the server to save the file to a network location and return that path to the user..
If you want to show a save as, add this to your ActionResult to indicate a file download:
Response.SetCookie(new HttpCookie("fileDownload", "true") { Path = "/" });
return myFileStreamResult
I needed to download and sort files into a rigidly defined directory structure on the client machine with no possibility of user mistakes. Ideally it would be completely automatic. I couldn't make it fully automatic, but in Chrome in Windows, I eliminated the possibility of typing mistakes with:
<a class="aDownload" href="file.txt" download="CTRL+V for suggested path/file">Download</a>
<textarea id="textareaClipboard"></textarea>
Using jQuery to listen for a click of the link, I call a function to generate the desired path and final file name, put it in the textarea, and transfer this to the clipboard using
jQuery('#textareaClipboard').select();
document.execCommand('copy' ,null ,null);
The Save As dialog pops up with "CTRL+V for suggested path/file" in the file name field. Follow the suggestion to paste the generated file name into this field, and hit Enter.
It requires a minimal amount of user action to ensure the file goes to the right directory with the right name, and the user can always reject the suggestion.
Your web application only can sending file to your client. its imposible to force download and save to spesific location, because download and save to privilege is belongs to client browser.
if user not defined default download location, it will prompt save to when download something, then if user already defined default download location. it will download automatically and save to default location.
so i think you have a little misconception with your web logic :D

Downloading files from 2 stage authentication protected server

I have to download some files from a server which has 2 stage authentication for a PowerPoint Addin I am trying to develop.
First, I log into the workspace through a browser...
In that browser I can call a .txt file and the contents are displayed in the browser - Great!
In my PowerPoint Addin I then have the following code for download a PP file and open it...
Globals.ThisAddIn.Application.Presentations.Open(#"https://workspace2.blahblah.com/group/corenarratives/Shared%20Documents/corenarratives/BlankPresentationTemplate.pptx");
This downloads the pptx file and opens it perfect! - Great!
I then try and download a .txt file with this code:
WebClient wc = new WebClient();
wc.DownloadFile("https://workspace2.blahblah.com/group/corenarratives/Shared%20Documents/corenarratives/rts.txt", #"C:\trev\trev.txt");
And the contents of the file contains a html error page...
When I save the .txt file as a .html file and open it in a browser it redirects me to the workspace login page...
I don't understand why the PowerPoint file opens and the .txt file doesn't?
And, how, if possible, to download the .txt file?
Can anyone help please?
Thanks
Is it ok to post an answer I would describe as "in progress"?
I know I guy on twitter who really knows his stuff.
Not a close friend but someone who I had followed for a long time. he wrote fiddler.
So, I stuck my neck out and asked him.
This is what he said.
"Watch your traffic from each scenario with Fiddler. Is PPT sending a Cookie, Auth header, or User-Agent your code needs to send?"
"WebClient isn't based on WinINET/URLMon. PowerPoint downloads (often) are, and that means it gets cookies, UA string, etc."
"PowerPoint has cookies and automatic authentication behaviors inherited from URLMon/WinINET."
Which, if I understand correctly, explains why PowerPoint can download a file..
I think.
Update:
I ended up implement this:
Is it possible to transfer authentication from Webbrowser to WebRequest
HTH

PDF does not reload on directory browsing

Hi guys this is my follow up question and I think this is the real issue here. Click Here
Whenever I access the file using the directory browsing (eg. http://localhost/temp/1.pdf) it always render the old or previous PDF file even if I change the entire file with the same file name, example I have 1.pdf with some content then I delete this file on the directory then replace with new and different content and rename it to 1.pdf then when I access it using browser it always render the previous value not the new one that I replace. This only happens on IE and Opera browser. Please need help this is a production issue.
Edit:
I found something very strange, example i have this url for the location of the pdf file (eg. http://localhost/website/tempfolder/1.pdf) the first pdf that I generated will show then some part in my code I change the casing of some letter example 't', I change it to 'T' then the newly pdf shows but when I revert back to the old case expecting that it will show the new pdf but sad to say the first PDF file shows.
Your browser is probably caching the PDF files.
For IE, you can do a CTRL F5 to force it to reload from web server.
Or put a random query string in your URL
e.g. /temp/1.pdf?v=1, /temp/1.pdf?v=2
Assuming you server is IIS, permanent solution is to configure HTTP response headers.
Go to IIS manager
Navigate to your folder
Click "HTTP Response Headers" in Features View
Right-button click/select "Set Common Headers"
Check "Expire Web Content" and select "Immediately".

Display Save File Dialog for a string ASP.NET C#?

I am generating a string, which right after generating I want to display a save dialog to allow this to be saved to the user's local machine. I don't want to save it to a file on the server, just have the user save it then clear out the string. Is this possible? I looked at SaveFileDialog but wasn't sure if it would accomplish this. Any help is appreciated, thanks!
You need to set certain headers to raise a file dialog box on the client. I think this is what you mean.
See here:
http://support.microsoft.com/kb/260519
You cannot show SaveFileDialog in the asp.net code because it is executed on the server side. You can send that generated string to user's browser with HTTP headers indicating that this is an attachment.

IE Information Bar, download file...how do I code for this?

I have a web page (asp.net) that compiles a package then redirects the user to the download file via javascript (window.location = ....). This is accompanied by a hard link on the page in case the redirect doesn't work - emulating the download process on many popular sites. When the IE information bar appears at the top due to restricted security settings, and a user clicks on it to download the file, it redirects the user to the page, not the download file, which refreshes the page and removes the hard link.
What is the information bar doing here? Shouldn't it send the user to the location of the redirect? Am I setting something wrong in the headers of the download response, or doing something else wrong to send the file in the first place?
C# Code:
m_context.Response.Buffer = false;
m_context.Response.ContentType = "application/zip";
m_context.Response.AddHeader("Content-Length", fs.Length.ToString());
m_context.Response.AddHeader("Content-Disposition", string.Format("attachment; filename={0}_{1}.zip", downloadPrefix, DateTime.Now.ToString("yyyy-MM-dd_HH-mm")));
//send the file
When a user agrees to download a file using the IE Information Bar, IE reloads the current page, not the page the file the user is trying to download. The difference is that, once the page is reloaded, IE will allow the download script to go through without prompting the user. I'm not sure what the thinking is on this from a design standpoint, but that's how it seems work.

Categories