Upload files in ASP.NET MVC
To upload a file using the HTML "Form" tag, the "method" attribute of the "Form" tag is "post", "enctype" attribute must be set to "multipart/form-data".
There is a need to set the path value at the "action" attribute, to use the DEXTUpload.NET Professional components, "use=dext" query string must be included in the path.
HTML representation is as follows.
<form action="/Sample/UploadSingle?use=dext" method="post" enctype="multipart/form-data"> <input type="file" name="file1" /> <input type="submit" value="Submit" /> </form>
When using Razor view engine in ASP.NET MVC environment, it can be expressed as follows.
@using (Html.BeginForm("UploadSingle", "Sample", new { use = "dext" }, FormMethod.Post, new { enctype = "multipart/form-data" })) { <input type="file" name="file1" /> <input type="submit" value="Submit" /> }
In the example, "UploadSingle" method of "SampleController" to handle the upload of the file is as follows.
[UseDEXT] public ActionResult UploadSingle([Bind(Prefix = "file1")] DEXTPostedFile file) { // If the file is empty, the "file" parameter will be "null". if (file != null) { // You can use the "SaveAs" method of the "file" object, but you can use the "file.Element" Properties to access the "FileElement" object, which provides more functionality. file.Element.Save(); } ... }
In ASP.NET MVC environment, it can access the uploaded files through the instance of the "HttpPostedFileBase" class. However, when using the DEXTUpload.NET Professional components, it must get the uploaded file from the "DEXTPostedFile" class instead of "HttpPostedFileBase".
"UseDEXTAttriubte" attribute class has same functionality as the " HttpPostAttribute" and in addition to examine whether there is the "use=dext" in the path or not. "UseDEXTAttriubte" does not always be used instead of the "HttpPostAttriubte". However, to use the delete function of temporary files that are provided with the product, use the "UseDEXTAttribute".
To use the DEXTUpload.NET Professional products in ASP.NET MVC environment, it is necessary to register the provider. If you don't register the provider, "file" parameter in "UploadSingle" action method is always to be "null", so calling "file.Element" property without checking "null" reference, the "NullReferenceException" exception occurs. The reason for "file" becomes "null" is, there is no process to bind the files form part of the multi-part data to "DEXTPostedFile" object. Therefore, it is necessary to register the "DEXTUpload.NET.Mvc.FileUploadValueProviderFactory" provider in order to bind the data of the file. (Even if properly register the provider, a part of the file is empty, the "file" parameter will be "null".)
Registration of providers is to be set in "Global.asax" file.
# Global.asax ... protected void Application_Start() { AreaRegistration.RegisterAllAreas(); RouteConfig.RegisterRoutes(RouteTable.Routes); // Add the provider for using DEXTUpload.NET Professional components in the ASP.NET MVC environmentIf receiving multiple files simultaneously, the action method of the controller is as follows.. ValueProviderFactories.Factories.Insert(0, new FileUploadValueProviderFactory()); }
If you want to use the parameter of the "HttpPostedFileBase" class, you should be noted that there is no "use=dext" query string in path.
If receiving multiple files simultaneously, the action method of the controller is as follows.
[UseDEXT] public ActionResult UploadMultiple(List<DEXTPostedFile> files) { // When receiving only one file, the parameter of the "DEXTPostedFile" may be "null", but the "files" parameter cannot be "null" because of the "List "<DEXTPostedFile>" generic object used when getting multiple files. // But note that this list object can have a "null" element. files.Where(f => f != null).Select(f => { f.Element.Save(); ... }); ... }