jdownload.aspx代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="jdownload.aspx.cs" Inherits="Scripts.jdownload.jDownload" %>
jdownload.aspx.cs
using System; namespace Scripts.jdownload { public partial class jDownload : System.Web.UI.Page { string[,] mime_types ={{"pdf", "application/pdf"},{"txt","text/plain"},{"html","text/html"}, {"htm","text/html"},{"zip","application/zip"},{"doc","application/msword"},{"docx","application/msword"}, {"xls","application/vnd.ms-excel"},{"xlsx","application/vnd.ms-excel"},{"ppt","application/vnd.ms-powerpoint"}, {"gif","image/gif"},{"png","image/png"},{"jpeg","image/jpg"},{"jpg","image/jpg"},{"rar","application/octet-stream"}}; string ext = null; string path = null; string action = null; protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { action = Request["action"];//download or info path = Request["path"];//file to be downloaded if (action != null && path != null && action.Length > 0 && path.Length > 0) { string absPath = Server.MapPath("~/" + path);// +"\\" + path.Replace("/", @"\"); System.IO.FileInfo fi = new System.IO.FileInfo(absPath); if (fi.Exists) { ext = fi.Extension.Replace(".", ""); bool bKnownExt = false; string contentType = "application/force-download"; for (int i = 0; i < mime_types.Length; i++) { if (ext.ToLower().Equals(mime_types[i, 0])) { bKnownExt = true; contentType = mime_types[i, 1]; break; } } if (!bKnownExt) { Response.Write("{\"error\":\"unknown file type.\"}"); Response.End(); } if (action.Equals("download")) { GetFile(absPath, contentType, fi.Length); } else { GetInfo(fi.Name, ext, fi.Length); } } else { Response.Write("{\"error\":\"File Not Exists.\"}"); Response.End(); } } else { Response.Write("{\"error\":\"Empty Param.\"}"); Response.End(); } } else { Response.Write("{\"error\":\"No repeat.\"}"); Response.End(); } } private void GetFile(string filePath, string contentType, long fileSize) { Response.ClearContent(); Response.AddHeader("Pragma", "public"); Response.AddHeader("Expires", "0"); Response.AddHeader("Cache-Control", "must-revalidate, pre-check=0"); Response.AddHeader("Content-Disposition", "attachment; filename=" + path); Response.AddHeader("Content-Type", contentType); Response.AddHeader("Content-Transfer-Encoding", "binary"); Response.AddHeader("Content-Length", fileSize.ToString()); Response.TransmitFile(filePath); } private void GetInfo(string fileName, string extension, long fileSize) { Response.Write("{\"filename\":\"" + Microsoft.JScript.GlobalObject.escape(fileName) + "\",\"filetype\":\"" + Microsoft.JScript.GlobalObject.escape(extension) + "\",\"filesize\":" + (fileSize / 1024).ToString() + "}"); } } }
jquery.jdownload.css
.jDownloadDialog { font-size: 14px; padding-top: 20px; } .jDownloadDialog .jDownloadInfo { margin-top: 20px; font-size: 12px; } .jDownloadDialog p { margin: 10px 0; width: 100%; } .jDownloadDialog .jDownloadInfo p span { font-weight: bold; width: 20%; display: inline-block; } .jDownloadDialog .jDownloadError { color: #dd0000; }
jquery.jdownload.js
/* * jDownload - A jQuery plugin to assist file downloads * Examples and documentation at: http://jdownloadplugin.com * Version: 1.3 (18/11/2010) * Copyright (c) 2010 Adam Chambers, Tim Myers * Licensed under the GNU General Public License v3: http://www.gnu.org/licenses/gpl.html * Requires: jQuery v1.4+ & jQueryUI 1.8+ */ //update wxf function GetUrl(path) { var dialog = $("#jDowndialog"); // set dialog options dialog.dialog({ autoOpen: false, buttons: { "Cancel": function () { $(this).dialog('close'); }, "Download": function () { start_download(path); $(this).dialog('close'); } }, width: 400, height: 300, modal: true, close: null }); dialog.html(""); // if filePath is not specified then use the href attribute var filePath = path; dialog.html('<p>Fetching File Info...</p><img src="../Scripts/jdownload/loader.gif" alt="Loading" />'); $.ajax({ type: 'POST', url: '../Scripts/jdownload/jdownload.aspx', data: 'action=download&path=' + filePath, error: function (XMLHttpRequest, textStatus, errorThrown) { dialog.html("<p class=\"jDownloadError\">Fatal Error.</p>"); }, success: function (data) { //setTimeout(function() { var jsonData={}; try { jsonData = JSON.parse(data); } catch(e) { } if (jsonData.error) { dialog.html("<p class=\"jDownloadError\">" + jsonData.error + "</p>"); } else { var url = '../Scripts/jdownload/jdownload.aspx?action=info&path=' + filePath; // get file information $.getJSON(url, function (filedata) { // Check to see if file is not allowed if (filedata.error == 'denied') { // append new file info dialog.html('<p class=\"jDownloadError\">This file type is not allowed.</p>'); } else { // parse JSON var html = "<div class=\"jDownloadInfo\">"; html += "<p><span>File Name:</span> " + decodeURI(filedata.filename) + "</p>"; html += "<p><span>File Type:</span> " + filedata.filetype + "</p>"; html += "<p><span>File Size:</span> " + ((filedata.filesize == "0") ? "<1" : filedata.filesize) + " KB</p>"; html += "</div>"; // remove any old file info & error messages $('.jDownloadInfo, .jDownloadError').remove(); var desc = 'Download the file now?'; // append new file info dialog.html('<p>' + desc + '</p>' + html); } }); } //}, 200); } }); // open dialog dialog.data('jDownloadData', { filePath: filePath }).dialog('open'); return false; }; function start_download(path) { $("#iframeDown").attr('src', '../Scripts/jdownload/jdownload.aspx?action=download&path=' + path); return false; } $(function () { var iframe = '<iframe id="iframeDown" class="jDownloadFrame" width="0" height="0" src="" style="visibility: hidden;"></iframe>'; $(iframe).appendTo("body"); var dialogHTML = '<div class="jDownloadDialog" title="DownLoad" id="jDowndialog"></div>'; var dialog = $(dialogHTML).appendTo("body"); });