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.
In the responses section, the Swagger UI provides a link for downloading the returned file. I can share the code separately if needed. Your email address will not be published. Notify me when reply to comments are added. November 17, NET Core. Example : Download or Return Image. NET Here we will be returning an image from the Controller method.
Loading Configuration. Hello Feixian. Sorry for the delayed response. Handle errors and improve performance. Publish your Drive app. Stay updated. Notice: important security update for Drive document URLs and resource keys.
For more information, refer to Access link-shared files using resource keys. Downloads of exported versions of Google Workspace files Google Docs, Sheets, Slides, and so on in formats that your app can handle. The rest of this guide provides detailed instructions for performing these types of downloads. The following code snippet shows how to download a file with the Drive API client libraries. The example uses an applink ID because this file is under a folder structure not owned by or shared with the current user.
The applink ID and access token are submitted in the request header. Previous Next JavaScript must be enabled to correctly display this content. Range optional : string The standard HTTP Range header allows clients to stream parts of the file rather than the entire file.
Example 2 The following example downloads version 1 of the specified file. Example 3 The following example downloads version 1 of the specified file. Example 4 The following example requests a download of version 1 of the specified file. Example 5 The following example requests a download of version 1 of the specified file.
0コメント