In particular, it won't have a file extension, so it won't be obvious how to open it. Normally when downloading from a server, the filename comes from the URL.
Even if not, then the content-disposition HTTP header can be used to override it and provide a better filename. But now we need the user to click on the link, unlike with window. For some applications this may be fine, but for the application this was developed for, the file was generated in response to a click on a button labeled Download , so it was best to avoid requiring another click.
When we want to return a file response, we can explicitly set the return type for the action method to be FileResult , which is a type inherited from ActionResult. But most times we still want to use the generic ActionResult because it covers many other useful return types, such as BadRequest , NoContent , and so on. In other words, no matter which of the four types you use, the client-side will not notice any difference when downloading a file from the API endpoint.
Apart from the constructors for the four concrete types, we can use a method File , provided natively from the ControllerBase class, to return a concrete FileResult object. With the groundwork laid, we can easily write an action method as follows:. In the code above, we first find or generate the requested file from the local file system or a file storage, then read the file content.
In the end line 7 , we return a FileContentResult object that is created using the method File byte[] fileContents, string contentType, string fileDownloadName. Note that we should validate the request and user permissions before finding or generating the requested file. And you can choose to stream the file too.
In the File method line 7 , the second parameter contentType is used in the HTTP header to indicate the format of contents transmitted over the internet. You can set the value for contentType according to your file type. In the File method, the third parameter fileDownloadName determines the value for the filename attribute in the content-disposition HTTP response header.
Browsers treat this value as the top priority to dictate the filename when downloading the file. DownloadFile filePath. CreateResponse HttpStatusCode. OK ; httpResponseMessage. FileName; httpResponseMessage. Add "x-filename", fileData. Prasad Shigwan Prasad Shigwan 5 5 silver badges 14 14 bronze badges. Combine uploads, file ; if!
DownloadProgress: this. Response: this. START: this. ERROR: this. Swati Swati 1 1 silver badge 6 6 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. Now the tricky part, where I lost plenty of time. We need to pipe the message as it is a readable stream to our writable stream. But the fact is that we need to wait until the close event is triggered.
This is where you need to wrap this up in the promise and wait for it to complete. In my example, I also look up for the error event and in case I do reject the promise. Believe it or not, considering my limited experience with JavaScript and TypeScript, I was not awaiting for those events and my code refused to work.
0コメント