If the IsSelected property is True i. CreateResponse HttpStatusCode. AddDirectoryByName "Files" ;. AddFile file. FilePath, "Files" ;. Save memoryStream ;. ToArray ;. Inside the View, the FileModel class is declared as IEnumerable which specifies that it will be available as a Collection. There is also Submit button which when clicked, the Form gets submitted.
Related Articles. 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.
Also, I have to save the files outside the project root directory. How can I implement this? Also I am using ASP. Net Core 3. For upload file - you should use interface IFormFile in your command and save Stream from that interface to eg. Before save you should check what is mime type and wheresome write information about file eg. For download file - you can use Method File in Controller. To saving file outside Project Root can be sometimes probaly.
Lot of external servers not shar that posibility. In my opinion should you save file in eg. Azure Blobs or simply in wwwroot in application. If you are passing the file back to your controller using HttpPostedFileBase , you can adapt the following code to suit your needs.
Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow. We cannot send the file from its original state. To send the file to REST service, we have to follow the below steps.
Initially, we have to convert the file into bytes using File Class which is from System. We are returning a file here so, to make the client understand the content of the service, we have to modify the following HttpResponseMessage header properties.
Below are a few examples of MIME — content types for your information. Please bookmark this page and share this article with your friends and Subscribe to the blog to get a notification on freshly published best practices of software development. This article was helpful. I did write my custom interface in the above code which returns the stream and achieved the result.
Hello James , Thanks for your query. I shall soon out the code on my GitHub. ReadAsStream id ;. Hello Ben, Thanks for your query.
0コメント