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.
Related
My web application (C# and ASP.Net) allows someone to upload a jpg/pdf file and save it as a memory stream inside a SQL table. This is what it looks like once written in the table:
0xFFD8FFE000104A4649.....
Now I want to provide this file back on my web interface through a link where the user can download this file. I have retrieved my file by converting the string above back to a byte array using
filedata = Encoding.ASCII.GetBytes(<string above>);
Then I called this:
string filename = "pic.jpg";
File.WriteAllBytes(filename, filedata);
But I have no clue how I should post this back to the user as a downloadable link on my web interface. Do I have to save this file to a temporary folder on my server or is there a way that I could invoke a call to render my file back as a picture where the user will be prompted to save the file or open it?
Thank you!
You will need to create a page / action / function that writes the bits back as the response to an HTTP request. Keep in mind that in doing so you will probably need to set the proper Content-Type header in the response according to what your file is.
So, you generate a link that calls your page / action / function. Then That sends the binary data back in an HTTP response. Something like
pic.jpg
If you some detail as to what framework you're using (MVC, WebForms, etc) then we can give more detailed examples.
Depending on how you want to present the image.
If you want to embed the image in the web return an link to the file with
string filename = Path.Combine(HttpRuntime.AppDomainAppPath, "Content/Images/pic.jpg");
File.WriteAllBytes(filename, filedata);
return filename;
then in the JavaScript side create an img element with thatsrc
Or if you want the user to download the file do what #squillman says in his answer
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
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".
I´m sending the value of a variable via POST to a PHP page in C#. I get the data stream from the server that has all the web page in HTML with the value of the POST. This information is stored in a string variable.
I would like to open a browser and show the web page (maybe using System.Diagnostics.Process.Start("URL")), without having to save it in a file, this is showing the page in the moment and, when the browser is closed, no file is stored in the server.
Any idea?
Drop a WebBrowser control into a new form webBrowser1 and set its DocumentTextProperty to your result html
webBrowser1.DocumentText = ("<html><body>hello world</body></html>");
source:
<html><body>hello world</body></html>
You aren't going to be able to do that in an agnostic way.
If you simply wanted to open the URL in a browser, then using the Process class would work.
Unfortunately, in your case, you already have the content from creating the POST to the server, and you really want to stream that response in your application to the browser.
It's possible among the some browsers, but it's not able to be done in an agnostic way (and it's complicated even when targeting a specific browser).
To complicate matters, you want the browser to believe that the stream you are sending it is really coming from the server, when in reality, it's not.
I believe that your best bet would be to save the response to the file system in a temp file. However, before you do, add the <base> tag to the file with the URL that the file came from. This way, relative URLs will resolve correctly when rendered in the browser.
Then, just open the temporary file in the browser using the Process class.
Hii,
My requirment is to show a dynamically created pdf file directly to my web page. It works fine for the system which is having pdf reader software. But for the system which does not have the pdf software it is showing error like below
The XML page cannot be displayed
Cannot view XML input using style sheet. Please correct the error and then click the Refresh button, or try again later.
An invalid character was found in text content. Error processing resource 'http://localhost:4252/OmanePost/Customer/EBox/PD...
I need to handle this situation bit differently.i.e In this situation the file should be save to the physical location of the system for that i need to identify whether the client machine has pdf software or not then i can manage properly
I m using ASP.NET 2.0 version
It looks to me that you are serving your PDF with an XML mime/content-type. Make sure you set your content-type to application/pdf and you'll probably get a more suitable browser response.
In this case the browser should ask the user to open the file in an external application.
Please verify that you are sending the correct Content-Type: application/pdf header. Certain versions of Microsoft's browser ignore the content-type header, so you need to specify a filename ending in .pdf in the content disposition header: Content-Disposition: inline; filename=filename.pdf;
Note: I have not verified that it works with "inline" instead of "attachment", but I think it is worth a try.
My requirment is to show a dynamically created pdf file directly to my web page.
Try online ZohoViewer that takes a PDF file link and displays in the browser without requiring PDF reader on the client machine. As such there's no way to check if the client machine has a pdf reader or not.
You can not identify that client system has pdf software using javascript, asp.net, c#.
If the PDF reader software is not there and the PDF is a valid PDF then it should not throw exception. Instead it asks for a software in client machine which can read the file.