Upload files by ASP.NET postback
ASP.NET is, basically, to develop with Web form style. When a user inputs to the Server Control, and the input data is passed to the server through the event, to render the Web form, and sends it to the client.
Thus, it's called Postback that the server control transmits(submit) the data for handling events, which is the basic method for developing a Web form.
In general, in order to transfer the file from a browser, sets the "type" attribute of the "input" element in the HTML to "file", sets the "method" attribute of the "form" element to "post" and the "enctype" attribute to "multipart/form-data".
<form action="..." method="post" enctype="multipart/form-data"> ... <input type="file" name="file1" /> <input type="submit" value="Submit" /> </form>
In contrast, the postback environment, there is no need to add two attributes("method, enctype") separately if a "asp:FileUpload" server control exists.
<form id="form1" runat="server"> ... <asp:FileUpload ID="file1" runat="server" /> <asp:Button ID="btnUpload" Text="Submit" runat="server" OnClick="btnUpload_Click" /> </form>
Rather than ASP.NET basic file upload processing methods, in order to use the DEXTUplod.NET Professional product, it must contain the "use=dext" query string in the request URL, it can be set as follows:
<form id="form1" action="?use=dext" runat="server"> ... <asp:FileUpload ID="file1" runat="server" /> <asp:Button ID="btnUpload" Text="Submit" runat="server" OnClick="btnUpload_Click" /> </form>
then, It processes the upload of the file in handler connected button events in the behind code(cs) of aspx.
protected void Page_Load(object sender, EventArgs e) { } protected void btnUpload_Click(object sender, EventArgs e) { Upload(); }
The "asp:FileUpload" server control's "ID" is "file1", it is possible to control the server control variable with the "file1" name in the behind code.
However, it is not possible to obtain the data of the file from this control rightly. Because the "DEXTUpload.NET.FileUploadMonitorModule" object intercepted the requested data intermediatly. Therefore, instead of the "file1" variable, it must be treated the file using the "DEXTUpload.NET.FileUpload" object.
using DEXT = DEXTUpload.NET.FileUpload; ... private void Upload() { // Because the "System.Web.UI.WebControls.FileUpload" and the "DEXTUpload.NET.FileUpload" are the same name, use the "import DEXT = DEXTUpload.NET.FileUpload;". using (var dext = new DEXT()) { // If you use PostBack, it is hard to know the form name correctly. (The server control's id does not match the form name one-to-one.) // So if you have only one file to upload, you can use the GetSingleFileElement method to get the uploaded file. // If you need to get the "FileElement" using the form name, you can use the "ClientID" of the server control. // var element = dext.GetFileElement (file1.ClientID); var element = dext.GetSingleFileElement(); if (!element.IsEmpty) { element.Save(); } } }
The name of the "DEXTUpload.NET.FileUpload" and "System.Web.UI.WebControls.FileUpload" care must be taken to the same when using the postback.