Download File Using Javascript/jQuery

前端 未结 28 2886
悲&欢浪女
悲&欢浪女 2020-11-21 05:11

I have a very similar requirement specified here.

I need to have the user\'s browser start a download manually when $(\'a#someID\').click();

But

28条回答
  •  一生所求
    2020-11-21 05:48

    These functions are used in stacktrace.js:

    /**
     * Try XHR methods in order and store XHR factory.
     *
     * @return  XHR function or equivalent
     */
    var createXMLHTTPObject = function() {
        var xmlhttp, XMLHttpFactories = [
            function() {
                return new XMLHttpRequest();
            }, function() {
                return new ActiveXObject('Msxml2.XMLHTTP');
            }, function() {
                return new ActiveXObject('Msxml3.XMLHTTP');
            }, function() {
                return new ActiveXObject('Microsoft.XMLHTTP');
            }
        ];
        for (var i = 0; i < XMLHttpFactories.length; i++) {
            try {
                xmlhttp = XMLHttpFactories[i]();
                // Use memoization to cache the factory
                createXMLHTTPObject = XMLHttpFactories[i];
                return xmlhttp;
            } catch (e) {
            }
        }
    }
    
    /**
     * @return the text from a given URL
     */
    function ajax(url) {
        var req = createXMLHTTPObject();
        if (req) {
            try {
                req.open('GET', url, false);
                req.send(null);
                return req.responseText;
            } catch (e) {
            }
        }
        return '';
    }
    

提交回复
热议问题