Download file from server to /public
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 ReadAllBytes was added years later in an edit. Why is this my second most upvoted answer? Oh well. Getting this error: non-invocable member "File" cannot be used like a method.
Show 10 more comments. For example using a virtual path to the file you could do the following. Octet, Path. GetFileName virtualFilePath ;. Jonathan Jonathan 1, 1 1 gold badge 8 8 silver badges 12 12 bronze badges. FileName , fileFromDB. FileName ;. That get Mime mapping is nice, but isn't it a heave process to figure out what is the type of file in run time?
MohammedNoureldin it is not "figuring" it, there is a simple mapping table based on file extensions or something like that. The server does it for all static files, it is not slow. I used it once and here's my code.
MapPath this. VirtualPath ; context. Bernard Vander Beken 4, 5 5 gold badges 47 47 silver badges 73 73 bronze badges. Manaf Abu. Rous Manaf Abu. Rous 2, 20 20 silver badges 24 24 bronze badges. Right, yes, I saw that article too, but it seems to do sort of the same thing as the article I used see the reference to my previous post , and he says himself at the top of the page that the workaround shouldn't be needed anymore because: "NEW UPDATE: There is no longer need for this custom ActionResult because ASP.
ManafAbuRous, if you read the code closely you will see it actually converts the virtual path to physical path Server. VirtualPath so consuming this directly without change is a tad naive. You should produce an alternate that accepts PhysicalPath given that's what is eventually required and is what you are storing. This would be much safer as you have made an assumption that the physical path and relative path would be the same excluding the root.
This is not accessible as a relative path. GetVirtualPath is great Thanks to Ian Henry! FileData; return File fileBytes, System.
Octet, myFile. DmitryBoyko DmitryBoyko What about client side, calling this method? Lets say if you want to show save as dialog? AppendHeader "Content-Disposition", cd. Servlet Tutorial. War File welcome-file-list Load on startup. Servlet Collaboration. RequestDispacher sendRedirect. Servlet Advance Session Tracking. ServletException; import javax.
Reinforcement Learning. R Programming. React Native. Python Design Patterns. Python Pillow. Python Turtle. Verbal Ability. Interview Questions. Company Questions.
Did this summary help you? Yes No. Log in Social login does not work in incognito and private browsers. Please log in with your username or email to continue. No account yet? Create an account. Edit this Article. We use cookies to make wikiHow great. By using our site, you agree to our cookie policy. Cookie Settings. Learn why people trust wikiHow. Download Article Explore this Article methods. Related Articles. Article Summary. Method 1. Create an HTML page if you don't have one yet.
You'll be adding your download link to an HTML web page. If you don't have an existing site, you can create a basic HTML page to test out the download link. Open a folder on your server for both the page and the file. The easiest way to link a file is by placing the file in the same folder as the page's HTML file. Use the control panel file manager or the file browser in your FTP program to navigate to the folder containing the HTML file you'll be adding the link to.
Your FTP client should already be configured to connect to your webserver since you've uploaded your site there before. If your web server has an online control panel, you can access your server files directly through the web management interface. You'll be able to access this when you sign into your site as an administrator.
Once you're in the control panel, select the "File Manager" option. If you created your website using a website creation tool like WordPress, Weebly, or Wix, see the platform-specific instructions in the methods below. Upload the file you want to create a link to. Note that some servers may limit the size of the file you are allowed to upload, and large files can eat through your allotted bandwidth pretty quickly.
To upload files using your FTP program, drag the file into the folder in the FTP window that you want to upload it to. It will begin uploading immediately. Your upload speeds will typically be much slower than your download speeds, so it may take a while to completely upload the file. If you are using the virtual control panel file manager, click the "Upload" button at the top of the window. Browse your computer for the file you want to upload. Large files may take a while to upload to your server.
Open the page you want to add your link to in your code editor. Once the file is uploaded, you'll need to add the link to it on your web page. Open the HTML file that you want to add the link to. You can double-click it in the control panel file manager to open it in the built-in page editor.
Find the spot on the page that you want to add the link. Place your cursor in the spot in the code that you want to insert your download link. This could be in the body of a paragraph, at the bottom of a page, or anywhere else.
Add the code for the link. Enter the following HTML5 code for your download link. This will start the download immediately for users after they click the link. As long as the file to be downloaded is in the same folder as the HTML file, you just need to use the name and extension.
If the file is located in another folder, you'll need to include the folder structure. Create a download button instead of a link. You can use an image instead of text to create the download link. This requires a button image already on your web server. Rename the downloaded file. This can make it easier for users to identify files that they download from you.
Save the changes to your HTML file. Once you're satisfied with your code, save the changes to your HTML file and reupload it if necessary. You'll be able to see your new download button live on your website. Method 2.