Manoj Ghediya Manoj Ghediya 4 4 silver badges 15 15 bronze badges. KZoeps KZoeps 65 1 1 silver badge 7 7 bronze badges. Sign up or log in Sign up using Google. Sign up using Facebook. Sign up using Email and Password. Post as a guest Name. Email Required, but never shown. The Overflow Blog. Podcast Who is building clouds for the independent developer? Exploding turkeys and how not to thaw your frozen bird: Top turkey questions Featured on Meta. Now live: A fully responsive profile.
Reducing the weight of our footer. Linked 0. Our HTTP request will eventually look like follows:. Since we don't just want to forward these events to every component, our service has to do some more work. Otherwise our component would have to deal with HTTP specifics - that's what services are for! Instead let's introduce a data structure representing a download with progress:.
A Download can be in one of three states. Either it hasn't started yet, therefore it's pending. Otherwise it's done or still in progress.
We use TypeScript's union types to define the different download states. Additionally, a download has a number indicating the download progress from 1 to Once a download is done, it will contain a Blob as its content - until then this property is not available, therefore null.
Now we want to abstract from specific HTTP events to our newly defined data structure. This way our components can be decoupled from the underlying network protocol. Since we're dealing with multiple events coming in over time, a RxJS operator is well suited here - so let's create one!
The first step for this will be the creation of type guards helping us to distinguish different HTTP events.
This way we can access event-specific fields in a type-safe way. They both contain the discriminator field type allowing us to easily return a boolean for the type assertion in our guards. The guards can be used with a simple if-statement, however, TypeScript will narrow the event type inside the statement block for us:.
Based on these guards we can now create our custom operator. It'll leverage scan , an operator that allows us to accumulate state for successive values coming through an observable. It takes up to two arguments: First, we provide an accumulator function which will compute the next Download state from the previous one and the current HttpEvent. Second, we'll pass a seed to scan representing the initial Download state. This seed will represent our download being pending without any progress or content:.
Our accumulator will use the previously defined guard to update the Download state over time with information from the HTTP events:. When we encounter a HttpProgressEvent , we calculate the progress based on the number of bytes already loaded and the total bytes. A download is done when we receive a HttpResponse containing the file contents in its body. When receiving any other events than HttpProgressEvent or HttpResponse , we won't alter the download's state and return it as it is. This way, for example, we can keep the information in the progress property while other events that won't allow us to compute the progress can be ignored for now.
When the service is provided at root level, Angular creates a single, shared instance of service and injects into any class that needs it. Registering the provider in the Injectable metadata also allows Angular to optimize an application by removing the service if it is not used.
If you are not using stricter type then your code should be working fine as it is working for Angular First replace the line this.
I am also accepting response as Blob Binary Large Object. You may also specify any value from supporting values, such as, json, blob, arraybuffer, text. You can have a look for more details on response type. I have used three ways for downloading file — two ways for Save as functionality and one way to show the file content on browser itself.
The above line create a Blob object with file content in response and expecting the file of JSON type. The above two lines create a URL that will open the file in browser in new window. The above line shows the file content on browser, so it does not give you save as option. The above line uses ready-made FileSaver module that will open the file with Save as option.
I have created service class to fetch file data from a server URL but I need to provide a link or button for downloading the file. In the view file I will give users two options for downloading the same file. I will use link as well as button for downloading the same file from the server. In the service class I have used Http module which may not be found automatically.
0コメント