转载自:http://www.codeproject.com/Articles/55488/File-Download-Using-JavaScript

Introduction

I know there are many developers looking for this kind of solution like I was looking. I had the same requirement to download the file from the server using JavaScript and with out postbacking the full page. The beauty of this is, it will work in IE/Firefox/Chrome browsers. I have not used ScriptManager/UpdatePanel anything rather I used Handler and normal JavaScript code. Handler is used to write the response back to the page. I have noted down the steps you may use in your programming. I can not use ActiveXObject in Firefox to create excel object in JavaScript because ActiveXObject will not work in Firefox.  

How Did I Do This?

  1. Add span tag on the page which will work as a link
  2. Create FileHandler.ashx to write the response back to the page.

 

You can use any MIME Type according to your requirement. I have showed some of the MIME types you can use in your application. For more MIME types, list http://msdn.microsoft.com/en-us/library/ms775147%28VS.85%29.aspx

 

 1 public enum MIMEType
 2 {
 3     [DescriptionAttribute("text/HTML")]TextHTML,
 4     [DescriptionAttribute("image/GIF")]Gif,
 5     [DescriptionAttribute("image/JPEG")]JPEG,
 6     [DescriptionAttribute("text/plain")]TextPlain,
 7     [DescriptionAttribute("Application/msword")]Word,
 8     [DescriptionAttribute("Application/vnd.ms-excel")]Excel,
 9     [DescriptionAttribute("Application/vnd.xls")]FirefoxExcel
10 }
11 public class MIMETypeUtility
12 {
13     public static string MIMETypeDescription(Enum mimeType)
14     {
15         FieldInfo fi = mimeType.GetType().GetField(mimeType.ToString());
16         DescriptionAttribute[] attributes = 
17                (DescriptionAttribute[])fi.GetCustomAttributes
18                 (typeof(DescriptionAttribute), false);
19         if (attributes.Length > 0)
20         {
21             return attributes[0].Description;
22         }
23         else
24         {
25             return mimeType.ToString();
26         }
27     }
28 }    
  1. Create FileHandler.js file to do the main work. It will create an iframe dynamically and which has width, height and border as 0. Then create form tag with one input control to pass filename (e.g. ExcelFile.xls) when you submit an iframe.
     1 var createFrame = function() 
     2 {
     3     var frame = document.createElement("iframe");
     4     frame.name = "fileFrame";
     5     frame.id = "fileFrame";
     6 
     7     document.body.appendChild(frame);
     8     generateIFrameContent();
     9     frame.style.width = "0px";
    10     frame.style.height = "0px";
    11     frame.style.border = "0px";
    12 }

     

  2. GetExcelFile function will pass Excel file name as parameter and will submit an iframe window and handler will run the code and writes the file response back to the page and the user will get open/save/cancel dialog window.
    1 var getExcelFile = function()
    2 {
    3     var frmWindow = getIFrameWindow();
    4     var fileName = frmWindow.document.getElementById("fileName");
    5     fileName.value = encodeURI("ExcelFile.xls");
    6     var frm = frmWindow.document.getElementById("frmFile");
    7     frm.submit();
    8 } 

     

  3. Create LoadScript.aspx page to add scripts to the page response. Here you can add any number of scripts and I prefer this way where I can hide the JavaScript and the location of the script file in page source at runtime.
       
  4. Add the following code to the FileHandler.aspx page to load scripts and startupscript with initialization of JavaScript function.
       

Conclusion

You can use AJAX (Scriptmanager + Updatepanel) to download the file from server, but you have to post back the page. This technique is light weight on the page. I had the requirement in my project to get the data from database and create an Excel file and insert the data and download the file to user machine. And my total website is using AJAX (JS+WebServices and no updatepanel), in this case I cannot use any server controls because that will post back my full page and I do not want that to happen. So after much R&D and many ways, this is one technique I have come up to use and it's working successfully in the production. 

Demo:

 

posted on 2013-05-21 16:17  草色年华  阅读(877)  评论(0编辑  收藏  举报