Oh well, I said that after the jQuery code I will explain how you can omit the enctype attribute, or if you even miss writing the enctype the result will be valid to upload the file.
There is no validation until now, you can attach your own validations depending on the MIME type or the size of the file that was attached to the request. The preceding code will work for any image or any file type. file.SaveAs(Server.MapPath("~/" + fileName)).Watch the jQuery code for more on this section. I know, a button is an old method, but in this method this button won't perform the built-in function, it will be captured by JavaScript and the page will stay where it is all using the JavaScript. For example, the following code would be an example of the HTML form that will accept the files from the user and contain a simple button from which he will upload the files. The first stage is to create the HTML form that will be capturing any of the file that the user wants to upload to the server. Ajax sends requests to the server, along with the file data that we want to be uploaded. In this article I will be using jQuery why? Because I love jQuery, it is shorter in syntax and looks cool, but remember it is slower than pure JavaScript because it is just a library that runs over JavaScript but the difference is barely noticeable. This makes the uploading of the content easy. This technology (Ajax) enables us to get data to the web server without having to use the POST requests and reload the web page for the new content and similarly it also enables us to send any data to the server, such as files, images and other data, to the web server. An Ajax request is an asynchronous request to the server to get some resources from the server and display it in the web page without needing to reload the web page. In JavaScript, we can shorten the code for creating an Ajax request. This option also minimizes the chances for error for any file that was triggered in the old methods and the user was informed of those errors after he uploaded all of the HTTP request data. You can manage which file to upload and which one to reject. But he can still view the results and other associated data, such as form elements and the result window.īut these are also old now, JavaScript is strong enough to upload your files and using HTML5 you can easily work your way with the validations of files too. Which means that if the form is submitted using an iframe then the main page the user is at is never submitted. The iframe is just another HTML document embedded in your main HTML document that if even navigated to any other web page it doesn't trigger a navigation event in the main page. Well before HTML5, people used hidden iframes to target the form to be uploaded from. The web is changing and there are many new ways for a user to upload an image or file to the server, without having to submit the form as he did back in 90s. Sometimes, the connection is lost and other stuff! This is a bit irritating and awkward, because the user uploaded more like 5 photos among them with one getting an error and the remaining 4 were also discarded and the user was told after 2 minutes to upload the 4 photos only and not that one specific image. That would let them select a file and then they will click on the Submit button that would then take them to a new page, where they will be shown the message, “Your photos have been uploaded”, or an error message telling them that there was an error. In the old days, people would create a simple HTML form and assign the enctype to it. Test it out.Most websites use HTML5 and jQuery plugins to upload files without requiring the user to send a POST request to the server but that is not a productive way to upload files. Then I set the text content of the span element(with the id of file-chosen) to the name property of the file object returned. A file object is returned which contains the details(such as name, file size etc) of the file uploaded. In the javascript file, I listen to the change event on the original file upload button(which we have hidden). What I did was to include a span tag (with an id of file-chosen) right after our custom file upload button. Unfortunately, we don't get to see that with our custom button. With the default file upload button, there is a no file chosen text beside the button (scroll up to the first codepen window), which gets replaced with the name of the file we will be uploading. Now we have this beautiful custom button, which actually works like the original file upload button:Īt this point, we are done. Enter fullscreen mode Exit fullscreen mode