Upload files
DEXTUpload.NET Professional is a server component that handles the request(multipart/form-data) that is encoded RFC1867 based on the HTTP protocol.
In general, in order to transfer the file in a browser, the "type" attribute of the element of "input" should be "file" in the HTML.
However, in the case where the submit in the form as a "post" type, it does not process the element of the "file" type, so there is a need to change the "enctype" attribute of the "form" element to "multipart/form-data".
<form action="..." method="post" enctype="multipart/form-data"> ... <input type="text" name="name" value="DEXTUpload.NET Professional"/> ... <input type="file" name="file1"/> ... </form>
Data transmitted in this way to the server, after automatically separates files and character strings in ASP.NET, they can be accessed individual files througth the "System.Web.HttpPostedFile" object.
"HttpPostedFile" object is provided as a item of the "Files" collection of the "System.Web.HttpRequest" class.
Rather than ASP.NET basic file upload function, to use the DEXTUpload.NET Professional component, first of all, there is a need to register the configuration information and HTTP module in the "Web.config" file. (For registration of configuration information and HTTP module, it is referring to the "Settings int the Web.config" and "<dextupload.net><settings> Settings" documents.)
In the module("DEXTUpload.NET.FileUploadMonitorModule") object registered in the "Web.config", separates the file part from the request(multipart/form-data) transmitted from the client and saves as a temporary file to a temporary storage.
The files that are stored in a temporary location finally save to the destination in the aspx page or ashx generic handler.
DEXTUpload.NET Professional product is responsible to the process of creating the temporary files automatically, user(programmer) only implements some code to move or copy files to final destination.
For the convenience to process the business logic dealing with temporary files in the aspx page or ashx generic handler, the product supplies the "DEXTUpload.NET.FileUpload" class.
// The "DEXTUpload.NET.FileUpload" is the class that processes uploaded files. using (var dext = new FileUpload()) { // Gets the string value passed as "name" name using the "GetString" method. var name = dext.GetString("name"); // Gets a "FileElement" object that can access the uploaded file using the "GetFileElement" method. var element = dext.GetFileElement("file1"); // If it is not an empty file if (!element.IsEmpty) { // Save the file to the final location. Here, "save" refers to the task of moving or copying the file in temporary storage to the destination. element.Save(); } }
For simply saving only files, the code such as the following ares possible.
using (var dext = new FileUpload()) { dext.SaveAll(); }
- Temporary folder and default folder
-
DEXTUpload.NET Professional, instead of locating the data of the uploaded file to the memory, writes in the temporary folder.
The job to write(move or copy) the file to actual storage is processed by calling "Save, SaveAs" methods of the "DEXTUpload.NET.IFileManagement" interface or "SaveAll" method of the "DEXTUpload.NET.FileUpload" class.
If you want to change the default folder, may change the value of "defaultPath" attribute of "settings" element of "dextupload.net" section in the "Web.config" file. Changing the configuration, however, it is effected to all requests, if you want to process different for each request, assign a path to the "DefaultPath" property of the "DEXTUpload.NET.FileUpload" object rather than changing the configuration.
using (var dext = new FileUpload { DefaultPath = "~/files/store/sub" }) { ... }
- To change the location and name of the file
-
Instead of the default path, to save the file to a different path, it specifies the path as a parameter in the "SaveAll, Save, SaveAs," methods.
// When the default save location is "~/files/store" and the file name is "photo.jpg" // Write "webapp root path\files\store\photo.jpg". element.Save(); // Write "webapp root path\files\other\photo.jpg". element.Save("~/files/other"); // Write "webapp root path\files\other\photo_new.jpg". element.SaveAs("~/files/other/photo_new.jpg");
The "SaveAll" method of the "FileUpload" can change the storage path such as the "Save" method of the "IFileManagement" interface.
If a file with the same name is present in the folder, a suffix(number) is attached after the file name.
- Gets the file element.
-
"DEXTUpload.NET.FileUpload" class support a variety of methods in order to get the form element of the file.
<form action="..." method="post" enctype="multipart/form-data"> ... <!-- Individual names --> <input type="file" name="file1"/> <input type="file" name="file2"/> <input type="file" name="file3"/> ... <!-- Same names --> <input type="file" name="files"/> <input type="file" name="files"/> <input type="file" name="files"/> </form>
using (var dext = new FileUpload()) { // Gets the first file element. var firstIndex = dext.GetFileElement(0); // Gets the object with the name "file1". var element1 = dext.GetFileElement("file1"); // Gets the object with the name "file2". var element2 = dext.GetFileElement("file2"); // Gets the object with the name "file3". var element3 = dext.GetFileElement("file3"); // Gets the first element with the "files" Name. var firstOfFiles = dext.GetFileElement("files"); // Gets all file elements("files, files, files") with the "files" Name. var listOfFiles = dext.GetFileElements("files"); // Gets all file elements("file1, file2, file3, files, files, files") var allFiles = dext.Collection.FileElements; // Get the first file element. = GetFileElement(0). var firstOfAll = dext.GetFirstFileElement(); // Get the last file element. var lastOfAll = dext.GetLastFileElement(); // Use this only if there is only one file element. var onlyOne = dext.GetSingleFileElement(); }
"GetFileElement" method returns the first item in the file form with same name if given a name as a parameter.