Mvc download file from directory
So, the purpose of posting this tip is to help the naive programmer like me to get the solution in the easiest way. It simply picks the file from one folder in application hierarchy, converts it into bytes and return the result as file to View.
Simply replace the FolderName with the folder in which file is present in project hierarchy, and replace filename. Simply call this in view using the line below:. Many code snippets are available on Google, but I found this one pretty interesting and straight forward for any programmer who is a kind of newbie like myself.
Do give me your feedback, either suggestions, corrections or praises. Sign in Email. Forgot your password? Search within: Articles Quick Answers Messages. Tagged as C. Stats VICK Rate me:. Then,use php code below;. Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. Asked 6 years, 3 months ago.
Active 6 years, 3 months ago. The Folder Directory is located within the Project. Below is the screenshot of the Solution Explorer window where you can see the Folder Directory i. Files and it containing three files. Following is a Model class named FileModel with one property i. You will need to import the following namespace. The Controller consists of two Action methods.
Action method for handling File Display operation. Later, the values of the Array are copied to a Generic List collection consisting objects of FileModel class and then returned to the View.
Action method for handling File Download operation. Related Articles. Add Comments. Thank you for the feedback. The comment is now awaiting moderation.
You will be notified via email when the author replies to your comment. Please select a comment to reply. You can add your comment about this article using the form below.
Make sure you provide a valid email address else you won't be notified when the author replies to your comment Please note that all comments are moderated and will be deleted if they are Not relavant to the article Spam Advertising campaigns or links to other sites Abusive content.
Please do not post code, scripts or snippets. Community Bot 1 1 1 silver badge. Anders Anders What you're doing is rather dangerous. You're pretty much allowing users to download any file from your server that the executing user can access. True - removing the file path, and nailing it down in the body of the actionresult would be somewhat safer. At least that way they only have access to a certain folder.
Are there any tools that allow you to find potentially dangerous loopholes such as this one? I find that it's convenient to set content-type as Response. GetMimeMapping filePath ; , from stackoverflow. What are you using on client side? Add a comment. Active Oldest Votes. Improve this answer. Tieson T. Ian Henry Ian Henry Ok, I could try that, but what goes into the byte[] array? Never mind, I think I figured it out. I read the filename full path into a FileStream and then into a byte array, and then it worked like a charm!
This loads the entire file into memory just to stream it out; for big files, this is a hog. A much better solution is the one below that doesn't have to load the file into memory first. As this answer is almost five years old, yeah. If you're doing this to serve very large files, don't.
If possible, use a separate static file server so you don't tie up your application threads, or one of many new techniques for serving files added to MVC since
0コメント