Get the docx file as a blob

When sending a docx file from the backend using Express, the code looks like this:

module.exports = (req, res) => {
  res.status(200).sendFile(__dirname+"/output.docx")
}

To download and save the file as a blob in Angular, the following code snippet is used:

 $http({
   url: '/api_cv/cv/gen',
   method: "PUT",
   responseType: 'blob'
}).success(function (data, status, headers, config) {
   var blob = new Blob([data], { type: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document' });
   var fileName = headers('content-disposition');
   saveAs(blob, 'file.docx');
}).error(function (data, status, headers, config) {
    console.log('Unable to download the file')
});

It successfully works in Chrome and Firefox. However, when tested in Safari, a new tab opens without downloading the file. In Internet Explorer (tested via Azure RemoteApp on a Mac), an error message stating, "your current security settings do not allow this file to be downloaded" appears.

The SaveAs function used is from https://github.com/eligrey/FileSaver.js/

Is there an alternative method that can work across all modern browsers and IE10+?

Answer №1

Instead of utilizing the saveAs function, have you considered trying the following approach:

var url = window.URL || window.webkitURL;
var downloadUrl = url.createObjectURL(blob);

var a = doc.createElement("a");
a.style.display = "none";

if (typeof a.download === "undefined") {
   window.location = downloadUrl;
} else {
   a.href = downloadUrl;
   a.download = fileName;
   doc.body.appendChild(a);
   a.click();
}

You can then remove the anchor element once you are done using it. I am curious if the issue lies in the saving process, as I haven't had much exposure to FileSaver. This is the method I have used previously with success.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Having trouble with Google Maps Places API autocomplete feature; it's not functioning properly

My goal is to integrate the Google Maps Places API with a search box. To test jQuery autocomplete, I managed to make it work using this simple example: function bindAutocomplete() { var locationSearch = $("input#Location"); locationSearch.autocom ...

Sending values from multiple radio groups in JavaScript can be done by accessing each group individually and extracting

This is an add to cart system. Currently, I am able to send quantity with an ID. However, I also want to send radio group values. How can I achieve this? Here are my codes: product.php <script> jQuery(function ($) { $('.popbutton').on(&a ...

Is it possible to restrict contenteditable elements to only accept numbers?

Is there a way to restrict contenteditable elements such that only numerical values can be entered? I attempted to use the following code snippet: onkeypress='return event.charCode >= 48 && event.charCode <= 57' However, despite a ...

Develop a new route that establishes connections with its related elements

I have successfully set up a MERN Stack application where I can perform GET, POST, and Delete operations on Topics and Posts separately. The two entities have a relationship defined in the Schema, but I am struggling to properly route it to incorporate the ...

retrieve the value of a text form using jQuery and its corresponding id

I need help with a basic HTML form that includes jQuery. My goal is to: 1) Retrieve the value into a JavaScript variable. 2) Send it as JSON to a server. Here is the form: <div data-role="main" class="ui-content"> <data=role "form" ...

How can we arrange a two-dimensional array in descending order of string length using the first string in the sub-array as the basis for

If I have an array with elements like these: var array = [["This should be last", 1], ["This should be first I think", 1], ["This is the middle one", 1]]; The second value in each sub-array, which is always 1 in this case, doesn ...

Errors occurred when trying to use Jquery to handle a JSON response

enter code here function customPaging(action, action1, page) { alert("in customPaging"); $.getJSON("./paging.do?action="+escape(action)+"&p="+escape(action1)+"&str="+escape(page), function(data){ alert("Length: "+data.length); var options = ...

Change the background of the play/stop icon with a jquery toggle

There is a list of video links with play icons as backgrounds in front of them. When a user clicks on a link, the video will start playing in a player located to the left of the links. The clicked link's background icon changes to a 'stop' i ...

Bringing an AMD module into a Mocha test

I recently encountered an error while using Mocha to test code that was exported as an AMD module. When running the Mocha test, I received the following error message: ReferenceError: define is not defined at Object.<anonymous> (/home/malintha/proje ...

Is there a quick way to use AJAX in Rails?

By using the remote:true attribute on a form and responding from the controller with :js, Rails is instructed to run a specific javascript file. For instance, when deleting a user, you would have the User controller with the Destroy action. Then, you woul ...

What is the best way to assign a series of radio buttons to an array within an Angular controller's model?

Let's say I have a controller that contains an array property named 'houses'. I want to use ng-repeat to display this array on a table row with a set of radio buttons (true/false, etc.). How can I ensure that selecting any of these radio but ...

Getting the Correct Nested Type in TypeScript Conditional Types for Iterables

In my quest to create a type called GoodNestedIterableType, I aim to transform something from Iterable<Iterable<A>> to just A. To illustrate, let's consider the following code snippet: const arr = [ [1, 2, 3], [4, 5, 6], ] type GoodN ...

Troubleshooting issues with jQuery `.live()` event not triggering as expected

In a project I am working on, I have implemented complex AJAX functionality to fetch inner page content from a WordPress blog. Due to the dynamic nature of the site, where the DOM is replaced after page load via AJAX, I have opted to use jQuery's .liv ...

Unable to save changes in AdminBro when using expressjs

After successfully implementing AdminBro in my express app, I encountered an issue where every POST request made by adminBro was not working properly. While fetching and deleting resources functioned as expected, updating/saving and creating resources resu ...

When using Windows Authentication in a Web API 2 application, the RequestContext.Principal may return the previous

For over a year, we've had an API built with Web API 2 (VB.Net) on IIS 7.5 that uses Windows Authentication and is accessed by an AngularJS app. Recently, we changed a user's username due to marriage, and although all normal Windows login process ...

Modifying Angular Select proves to be a challenge once ng-show is in effect

Encountered a familiar problem with ng-src, but now facing difficulty dealing with it using an <select>. It seems simple, but once the select dropdown appears, changing the value becomes impossible. Attempted solutions like $apply() and $compile() ha ...

Using JQuery to reverse the action of hiding an image

Having some trouble with my Javascript and JQuery skills, so bear with me. I've set up a gallery of images where users can drag and drop them into a designated drop zone. Everything works fine when dragging an image from the gallery to the drop zone a ...

Verifying the credentials entered in a form with the data stored in a MongoDB database in order to grant access to a different page using Node.js and Express

As a beginner working on a project using nodejs, express, and mongo db, my goal is to create a scenario where users do not have to register or sign up. Instead, they are provided with a username and password by the administrator, stored in mongodb. To ac ...

Exploring the process of integrating with Elasticsearch to retrieve data in a GraphQL Apollo 4 environment

I seem to be facing a major roadblock, my friends. I am struggling to figure out how to send data from my database to GraphQL. Even when attempting a POST request, all I get back is NULL! Here's my code spread across 5 different files. First up, we h ...

Discover the process of attaching an event to the keyboard display within a Cordova application

I've exhausted my efforts trying to figure out how to assign an event for when the virtual keyboard appears on my hybrid cordova app. I'm looking to trigger a specific action whenever the keyboard shows up in my app consistently. ...