Javascript 通过jqueryajax从httpurl下载文件

Javascript 通过jqueryajax从httpurl下载文件,javascript,jquery,html,ajax,Javascript,Jquery,Html,Ajax,我在html页面上有输入类型文本。我想粘贴URL,在这个文本字段中的示例上,单击“下载”按钮并将此文件下载到我的计算机上。 我想用AJAX实现这一点。也许您知道一些AJAX插件或代码可以实现从HTTP URL下载文件?您不需要AJAX来实现这一点,因为CORS的限制,您也无法做到这一点。相反,试试这样的方法 function downloadme(x,y){ myTempWindow = window.open(x,'','left=10000,screenX=10000');

我在html页面上有输入类型文本。我想粘贴URL,在这个文本字段中的示例上,单击“下载”按钮并将此文件下载到我的计算机上。
我想用AJAX实现这一点。也许您知道一些AJAX插件或代码可以实现从HTTP URL下载文件?

您不需要AJAX来实现这一点,因为CORS的限制,您也无法做到这一点。相反,试试这样的方法

function downloadme(x,y){

        myTempWindow = window.open(x,'','left=10000,screenX=10000');
        myTempWindow.document.execCommand('SaveAs','null',y);
        myTempWindow.close();
    }
HTML:

function downloadme(x,y){

        myTempWindow = window.open(x,'','left=10000,screenX=10000');
        myTempWindow.document.execCommand('SaveAs','null',y);
        myTempWindow.close();
    }

您不需要AJAX来实现这一点,因为CORS的限制,您也无法做到这一点。相反,试试这样的方法

function downloadme(x,y){

        myTempWindow = window.open(x,'','left=10000,screenX=10000');
        myTempWindow.document.execCommand('SaveAs','null',y);
        myTempWindow.close();
    }
HTML:

function downloadme(x,y){

        myTempWindow = window.open(x,'','left=10000,screenX=10000');
        myTempWindow.document.execCommand('SaveAs','null',y);
        myTempWindow.close();
    }

您不需要AJAX来实现这一点,因为CORS的限制,您也无法做到这一点。相反,试试这样的方法

function downloadme(x,y){

        myTempWindow = window.open(x,'','left=10000,screenX=10000');
        myTempWindow.document.execCommand('SaveAs','null',y);
        myTempWindow.close();
    }
HTML:

function downloadme(x,y){

        myTempWindow = window.open(x,'','left=10000,screenX=10000');
        myTempWindow.document.execCommand('SaveAs','null',y);
        myTempWindow.close();
    }

您不需要AJAX来实现这一点,因为CORS的限制,您也无法做到这一点。相反,试试这样的方法

function downloadme(x,y){

        myTempWindow = window.open(x,'','left=10000,screenX=10000');
        myTempWindow.document.execCommand('SaveAs','null',y);
        myTempWindow.close();
    }
HTML:

function downloadme(x,y){

        myTempWindow = window.open(x,'','left=10000,screenX=10000');
        myTempWindow.document.execCommand('SaveAs','null',y);
        myTempWindow.close();
    }
第一种方法

function SaveToDisk(fileURL, fileName) {
                //alert("yes i m working");
                // for non-IE
                if (!window.ActiveXObject) {
                    var save = document.createElement('a');
                    save.href = fileURL;
                    save.target = '_blank';
                    save.download = fileName || 'unknown';

                    var evt = new MouseEvent('click', {
                        'view': window,
                        'bubbles': true,
                        'cancelable': false
                    });
                    save.dispatchEvent(evt);

                    (window.URL || window.webkitURL).revokeObjectURL(save.href);
                }

                // for IE < 11
                else if ( !! window.ActiveXObject && document.execCommand)     {
                    var _window = window.open(fileURL, '_blank');
                    _window.document.close();
                    _window.document.execCommand('SaveAs', true, fileName || fileURL)
                    _window.close();
                }
           } 
function downloadme(x,y){

        myTempWindow = window.open(x,'','left=10000,screenX=10000');
        myTempWindow.document.execCommand('SaveAs','null',y);
        myTempWindow.close();
    }
第一种方法

function SaveToDisk(fileURL, fileName) {
                //alert("yes i m working");
                // for non-IE
                if (!window.ActiveXObject) {
                    var save = document.createElement('a');
                    save.href = fileURL;
                    save.target = '_blank';
                    save.download = fileName || 'unknown';

                    var evt = new MouseEvent('click', {
                        'view': window,
                        'bubbles': true,
                        'cancelable': false
                    });
                    save.dispatchEvent(evt);

                    (window.URL || window.webkitURL).revokeObjectURL(save.href);
                }

                // for IE < 11
                else if ( !! window.ActiveXObject && document.execCommand)     {
                    var _window = window.open(fileURL, '_blank');
                    _window.document.close();
                    _window.document.execCommand('SaveAs', true, fileName || fileURL)
                    _window.close();
                }
           } 
function downloadme(x,y){

        myTempWindow = window.open(x,'','left=10000,screenX=10000');
        myTempWindow.document.execCommand('SaveAs','null',y);
        myTempWindow.close();
    }
第一种方法

function SaveToDisk(fileURL, fileName) {
                //alert("yes i m working");
                // for non-IE
                if (!window.ActiveXObject) {
                    var save = document.createElement('a');
                    save.href = fileURL;
                    save.target = '_blank';
                    save.download = fileName || 'unknown';

                    var evt = new MouseEvent('click', {
                        'view': window,
                        'bubbles': true,
                        'cancelable': false
                    });
                    save.dispatchEvent(evt);

                    (window.URL || window.webkitURL).revokeObjectURL(save.href);
                }

                // for IE < 11
                else if ( !! window.ActiveXObject && document.execCommand)     {
                    var _window = window.open(fileURL, '_blank');
                    _window.document.close();
                    _window.document.execCommand('SaveAs', true, fileName || fileURL)
                    _window.close();
                }
           } 
function downloadme(x,y){

        myTempWindow = window.open(x,'','left=10000,screenX=10000');
        myTempWindow.document.execCommand('SaveAs','null',y);
        myTempWindow.close();
    }
第一种方法

function SaveToDisk(fileURL, fileName) {
                //alert("yes i m working");
                // for non-IE
                if (!window.ActiveXObject) {
                    var save = document.createElement('a');
                    save.href = fileURL;
                    save.target = '_blank';
                    save.download = fileName || 'unknown';

                    var evt = new MouseEvent('click', {
                        'view': window,
                        'bubbles': true,
                        'cancelable': false
                    });
                    save.dispatchEvent(evt);

                    (window.URL || window.webkitURL).revokeObjectURL(save.href);
                }

                // for IE < 11
                else if ( !! window.ActiveXObject && document.execCommand)     {
                    var _window = window.open(fileURL, '_blank');
                    _window.document.close();
                    _window.document.execCommand('SaveAs', true, fileName || fileURL)
                    _window.close();
                }
           } 
function downloadme(x,y){

        myTempWindow = window.open(x,'','left=10000,screenX=10000');
        myTempWindow.document.execCommand('SaveAs','null',y);
        myTempWindow.close();
    }


浏览器通常非常复杂,足以阻止通过生成的鼠标事件单击的锚定标记,请注意此限制。第一种方法有效,谢谢,但在这段代码中,我如何更改目录以保存文件?@mvoron-这是强制下载好友。@mvoron-检查您的浏览器下载文件夹。如果需要,您可以更改它。例如。谢谢你的第一个方法。它将文件保存为“未知”我将其更改为fileURL它将其保存为“http--localhost-ieltsmaster-gtreading-workbook-Activity_3_1.pdf”知道如何修复它吗?浏览器通常非常复杂,可以阻止通过生成的鼠标事件单击锚定标记,请注意此限制。第一种方法有效,谢谢,但在这段代码中,我如何更改目录以保存文件?@mvoron-这是强制下载好友。@mvoron-检查您的浏览器下载文件夹。如果需要,您可以更改它。例如。谢谢你的第一个方法。它将文件保存为“未知”我将其更改为fileURL它将其保存为“http--localhost-ieltsmaster-gtreading-workbook-Activity_3_1.pdf”知道如何修复它吗?浏览器通常非常复杂,可以阻止通过生成的鼠标事件单击锚定标记,请注意此限制。第一种方法有效,谢谢,但在这段代码中,我如何更改目录以保存文件?@mvoron-这是强制下载好友。@mvoron-检查您的浏览器下载文件夹。如果需要,您可以更改它。例如。谢谢你的第一个方法。它将文件保存为“未知”我将其更改为fileURL它将其保存为“http--localhost-ieltsmaster-gtreading-workbook-Activity_3_1.pdf”知道如何修复它吗?浏览器通常非常复杂,可以阻止通过生成的鼠标事件单击锚定标记,请注意此限制。第一种方法有效,谢谢,但在这段代码中,我如何更改目录以保存文件?@mvoron-这是强制下载好友。@mvoron-检查您的浏览器下载文件夹。如果需要,您可以更改它。例如。谢谢你的第一个方法。它将文件保存为“未知”我将其更改为fileURL它将其保存为“http--localhost-ieltsmaster-gtreading-workbook-Activity_3_1.pdf”知道如何修复它吗?