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.
Related
I got a requirement to create pdf on the fly from the data stored in database.
I am using html, jquery and WCF in my application.
I don't find a way to generate pdf (Show in a browser or as an attachment) using client technology (jquery, or any other client plugins). I tried to use pdf.js, but could not able to succeed. Later I used .net generic handler to generate pdf. I was passing bytearray to handler, in turn pdf started rendering on client.
I got some random issue, then I wrote a sample application to make sure handler is working fine. I used same code with a static text to generate pdf, but it started throwing a error on the client.
Below is the code snippet I used
context.Response.Clear();
context.Response.ContentType = "application/pdf";
context.Response.AddHeader("content-disposition", "inline; filename=download");
context.Response.Write("Hello World");
//context.Response.BinaryWrite(System.Text.Encoding.Default.GetBytes("Hello world"));
//context.Response.BinaryWrite(System.Text.Encoding.UTF8.GetBytes("Hello world"));
context.Response.Flush();
context.Response.End();
I am getting errors on all the browsers
Failed to load pdf document on Chrome.
An error occurred while loading the PDF. PDF.js v0.8.505 (build: da1c944) Message: InvalidPDFException - In mozilla firefox
File does not begin with '%PDF-' in IE.
Note: I tried with aspx page too. I cant create physical file due security issues. If there is any better way of achieving it please let me know. Thanks in advance.
PDF is a standard file format.You cannot write a normal text file and rename it to filename.pdf and wish it to work.
You will need .Net libraries for that.There are lot try googling .Net pdf library
Some Info.
pdf.js is just for viewing in browser.
setting context.Response.ContentType just tells the browser the file its gonna receive,So it can use existing mapped application to open it.Like Adobe browser plugin for pdf file.
I am working on a video upload module, which ultimately stores all the user uploaded video's on the youtube (using youtube api's).
I want to display a progress bar as soon as user click's on upload button and continue showing until the video is uploaded.
I know generally this is achieved using ajax toolkit in .net but since I am using File Upload control, its not compatible with the ajax toolkit.
Kindly suggest me how I achieve this.
You can not show the actual progress of the file upload. But you can show a hidden animated "loading" image with javascript. It will show while the form is submitted (with the file) to the server.
Html:
<img id="loading_img" style="Display: None" src="loading.gif" />
Javascript:
$('#Form1').submit(function() {
$('#loading_img').show(); // Show image
return true; // Proceed with postback
});
A while ago I needed exaclty that, I first did search for a javascript solution but ended up with a Flash uploader. Some browsers (Chrome, Firefox) report uploading status if you're uploading through the XMLHttpRequest API in Javascript, so it definitely can be done, but this method is (or was at the time I checked it out) not cross-browser.
You can check this example and also this Q & A.
If that doesn't work cross-browser, you can use an existing Flash file uploader or create one your own using Flash or Silverlight. This, of course, makes your site Flash or Silverlight dependent.
This, for example, is a project that uses Flash.
Fastest way to do that is to buy 3rd party control, for example Telerik Upload control can do that (no flash)
http://demos.telerik.com/aspnet-ajax/upload/examples/customizingraduploadui/defaultcs.aspx
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 ;-)
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.
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