简体   繁体   中英

Unable to download the file from Node.js using the POST request

I found a few questions related to the same but the answer didn't work for me so posting the same here.

I have some XML content which has been created programmatically and displayed in Textarea. I need an option to export or download the same to the local system. So I am using the POST request to send these data to NODE.JS where I am creating my XML file and trying to download it. I know if I am using the GET it would work directly but with POST it's failing. Is there a way I can do this?

Here is the code I have:

Angularjs POST request: I have a button when clicked I am passing all the XML data to my NODE.js function:

$scope.ExportXML    =   function(XMLContent){
    var XMLContent  =   XMLContent;
    
    $http({
        url     :   "/ExportData",
        method  :   "POST",
        data    :   XMLContent
    }).success(function(data, status, headers, config) {
        console.log(data);
        console.log("Data Exported");
        window.location.assign(data);
        $window.open(data);
    }).error(function(error, status, headers, config) {
        console.log('ERROR: could not download file');
        console.log(error)
    });
}

My Node.js function which would create the XML file with the data:

const fs        =   require('fs');
const path      =   require('path');
const reqPath   =   path.join(__dirname, '../');

exports.exportfile      =   function(req,res)
{   
    var ContentData     =   req.body;
    var FileName        =   "XMLDATA.xml";  
    
    fs.appendFile(FileName, ContentData, function (err)
    {
        const FilePath = reqPath+FileName;
        res.download(FilePath);
    })
}

As you can see from the success function of the ANGULARJS I tried a couple of things but none worked. I tried sending back the path of the file using the callback(FilePath); and then I am trying to download the file using the $window.open(data);but I am getting the following error Not allowed to load local resource: .

Can someone please help me with this?

After a bit of searching I was able to do it. Rather than sending the DATA to NODE.JS using the POST, I tried creating the file within my AngularjS function and downloaded it from there itself. if in case anyone is looking for the solution here it is:

//Export the contents of XML to text file
$scope.ExportXML    =   function(XMLContent){
    
    var filename = 'HelloWorld.xml'       
    var blob = new Blob([XMLContent], {type: "text/xml"});
    if (window.navigator && window.navigator.msSaveOrOpenBlob)
    {
        window.navigator.msSaveOrOpenBlob(blob, filename);
    }
    else
    {
        var e                   =   document.createEvent('MouseEvents'),
        a                       =   document.createElement('a');
        a.download              =   filename;
        a.href                  =   window.URL.createObjectURL(blob);
        a.dataset.downloadurl   =   ['text/json', a.download, a.href].join(':');
        e.initEvent('click', true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
        a.dispatchEvent(e);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM