I am dynamically generating html file. It is formated with Css and Images. I am looking for code which should able to execute on button click, and ask the location for saving file. also it should download concern resource files which has referenced in html file. What code i have to do ? Can i use WebClient for the same ?
There is no "Save As" dialog in ASP.NET. Since it is running in a browser. You have no access to the user's file system, including the Save As dialog.
But you can send the user a file, as an attachment, most browsers will display a dialog asking the user whether to save the file or open it. Maybe the user will choose to save it.
Response.ContentType = "application/octet-stream" (or content type of your file).
Response.AppendHeader("content-disposition", "attachment;filename=" & strFileName)
You could create a situation where you can use WebClient if you make your file available through a uri, see this post.
Yes, you can use WebClient. Option is to use HttpRequest and HttpResponse.
Related
I have some images in my Asp.net web page. My requirement is to save a selected image to users disk while clicking a save button.Normally all browser has the provision for right click save as option.But I have to implement this feature on an html input button click. I have the following questions.
How can I make a script to do this functionality which should work on all major browsers
Do we get any jQuery library which does this functionality
Can I get any alternative in asp.net technology rather than java script/jQuery for doing the same functioanlity
Can anyone please help me?
jQuery is a client-side javascript library. It is not involved with serving files to download.
You can provide a download on any button click in asp.net. jQuery is not required for downloading files.
Your code in the event handler would look as follows:
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", string.Format("attachment; filename=\"{0}\"", fileName));
Response.WriteFile(filePath + fileName);
Response.Flush();
Response.End();
You cannot write a file straight to disk from a website, period. That's just sensible security. A regular download link/button will always invoke the standard download process of the browser, in which you have no say whether the file will be saved at all or where to. The only way to circumvent that is to create a native browser plugin which the user will have to install first, which will write files from your site straight to a specific location on disk. Please be extremly careful writing such a plugin should you do so so it does not become a gaping security hole for everybody.
I am saving a .doc file to a SQL Server database as varbinary(max) using the C# code below.
I am able to save the file, but when I retrieve the file back and want to display the contents on the web page, my code is downloading the file and I am in great confusion about how to handle it.
The exact functionality I am looking for is the way naukri.com uploads the resume and gives a preview of it. My code is :
byte[] fileContent = new byte[fuResume.PostedFile.ContentLength];
fuResume.PostedFile.InputStream.Read(fileContent, 0, fuResume.PostedFile.ContentLength);
//lblAppliedMessage.Text = ByteArrayToString(fileContent);
//lblAppliedMessage.Text = BitConverter.ToString(fileContent).Replace("-", string.Empty);
byte[] btYourDoc;
btYourDoc = fileContent;
Response.ContentType = "application/ms-word";
Response.AddHeader("Content-Disposition", "inline;filename=yourfilename.doc");
Response.OutputStream.Write(btYourDoc, 0, fileContent.Length);
Response.BinaryWrite(btYourDoc);
Response.End();
The reason your file is getting downloaded instead of displayed is because you're setting the content type to application/ms-word. This tells a browser to download the file (they can't natively handle files of that type so they delegate to an external app).
You'll need to have code that knows how to interpret the MS Word format and convert that to something viewable in a browser (HTML, some kind of plugin that will do that for you, etc). Saving the raw Word document and then sending it back to the client in the same state is basically just having them download a Word file.
squillman is right. There are tons of third party components that do Word -> HTML conversion.
One other option, which may be more appropriate for an intranet site, is to install Word on the server.
An example of this is here:
http://www.c-sharpcorner.com/UploadFile/munnamax/WordToHtml03252007065157AM/WordToHtml.aspx
Effectively, the doc is opened, saved out as HTML, then subsequent requests can retrieve the HTML version of the file for preview.
Office automation server side has many pitfalls, however - see http://support.microsoft.com/kb/257757 for more information.
Here's a good one where the end result it's up to the user whether to download or view the file here's the link but #Squillman is right by putting the Response headers you're telling it to download.
I've created a word document generator in C sharp and I want that generated word document to be save in a specific place in the client. I'm looking for a similar functionality like FolderBrowserDialog in Windows Form. I'm using ASP.Net and I tried may solutions but still no luck. Anyone can help me.
No! server (web-app) program don't have an ability to save a generated document at specific place at client.
try to use the HttpResponse in behind's code:
like:
// Clear the content of the response
Response.ClearContent();
// Add the file name and attachment, which will force the open/cancel/save dialog box to show, to the header
Response.AddHeader("Content-Disposition", "attachment; filename=" + savedNameWithExtension);
// Add the file size into the response header
Response.AddHeader("Content-Length", myfile.Length.ToString());
// Set the ContentType
Response.ContentType = ReturnExtension(myfile.Extension.ToLower());
// Write the file into the response (TransmitFile is for ASP.NET 2.0. In ASP.NET 1.1 you have to use WriteFile instead)
Response.TransmitFile(myfile.FullName);
// End the response
Response.End();
This section of code will prompt the user to save the specified file on a certain location on his machine.
Hope this is clear.
When you download a file from the browser its the browser to show the save file dialog and this works on all browsers and on all platforms. Just keep the default behavior and let users decide location and file name. I guess any hacky implementation of that part if possible at all, it is likely to break in some browsers or platforms, like mac, ipad, android...
you do not need to specify any control to be used, once you call the methods to download a file like
Response.WriteFile or Response.BinaryWrite or any other, the browser takes care of everything else for you and let it be like that ;-)
The prompt that we usually get when downloading a file from a website using the IE is hampering the download of a tar file for me .
I am using C# .
Here is the code snippet .
WebClient wc = new WebClient();
wc.DownloadFileAsync(#"www.somelink.com", #"C:\a.tgz");
What i get is not a valid a.tgz file.
I am assuming its downloading some content related to the save/open dialog given by the internet browser .
How do I by pass it ?
Are you downloading the entire file? It could be the download is being interrupted prematurely.
The save/open dialog is not a problem, as that is IE's method of displaying non-HTML content. However if the website is responding with a webpage then that could be your problem (for instance "You need to be logged in to proceed" or "Click here to download the file").
Try renaming the file to html and see if you can read it then. Alternatively you can load it in notepad to see if it is plaintext if you are afraid of what the contents are for any reason.
EDIT:
You may also not be fully downloading the file, given the description on MSDN of the methodology, an empty file is placed at the target location, the file is downloaded to a temporary location, then finally it is moved to the target, if the download is being interrupted, by say the program exiting, that would explain the problem you are experiencing.
Short version: Try downloading synchronously and see if that works better.
how to get file on asp.net form so that user can download it?
suppose i have created one excel file and i want to upload it on form so that user can download it, fill the details when they are offline.
You can use
Response.AddHeader("content-disposition", "attachment; filename=test.txt");
Response.WriteFile(#"test.txt");
Response.End();
Otherwise if it's a specific file you can use a normal Download Meand point it to the files location.
Use the fileupload component. I would store the file locally on the server and then display a list of the uploaded files.the list would be a list of hyperlinkswhich you can link directly to the uploaded files so when the user clicks on one, the file download begins automatically using the standard browser behavior.
Place the file in one of your directories under the application root folder and then you can simply place an anchor tag to let user download the file, like
abc