Example T365088
Visible to All Users

dxFileUploader - How to upload files to the server in an ASP.NET Web Forms application

Files to look at:

This example demonstrates how to upload files to the server by using the dxFileUploader widget in an ASP.NET WebForms application. Two approaches are illustrated:

  1. Uploading files by using a custom HTTP Handler.
    2) Uploading files when the form is submitted.

See also:
T365089: dxFileUploader - How to upload files to the server in an ASP.NET MVC application

Does this example address your development requirements/objectives?

(you will be redirected to DevExpress.com to submit your response)

Example Code

Upload/Default.aspx
ASPx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Upload.Default" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> <link rel="stylesheet" type="text/css" href="http://cdn3.devexpress.com/jslib/15.2.7/css/dx.common.css" /> <link rel="stylesheet" type="text/css" href="http://cdn3.devexpress.com/jslib/15.2.7/css/dx.light.css" /> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script> <script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/globalize/0.1.1/globalize.min.js"></script> <script type="text/javascript" src="http://cdn3.devexpress.com/jslib/15.2.7/js/dx.webappjs.js"></script> </head> <body> Http Handler Uploading: <div id="uploadContainer"></div> Form Submit Uploading: <form id="form1" enctype="multipart/form-data" runat="server"> <div id="formUploadContainer"></div> <input type="submit" /> </form> <script type="text/javascript"> $("#uploadContainer").dxFileUploader({ buttonText: 'Select file', labelText: 'Drop file here', multiple: true, accept: 'image/*', uploadUrl: 'http://localhost:62539/Uploader.ashx' }); $("#formUploadContainer").dxFileUploader({ buttonText: 'Select file', labelText: 'Drop file here', multiple: true, accept: 'image/*', uploadMode: 'useForm' }); </script> </body> </html>
Upload/Default.aspx.cs(vb)
C#
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace Upload { public partial class Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (IsPostBack) { SaveFilesHelper.Save(this.Context); } } } }
Upload/SaveFilesHelper.cs(vb)
C#
using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace Upload { public class SaveFilesHelper { public static void Save(HttpContext context) { try { HttpFileCollection files = context.Request.Files; HttpRequest request = context.Request; if (files.Count != 0) { for (int i = 0; i < files.Count; i++) { HttpPostedFile file = files[i]; if (file.ContentLength != 0) { string fileName = string.Empty; if (request.Browser.Browser.Contains("InternetExplorer")) fileName = System.IO.Path.GetFileName(file.FileName); else fileName = file.FileName; string path = context.Server.MapPath("~/Images/") + fileName; file.SaveAs(path); } } } } catch (Exception ex) { context.Response.Write(ex.Message); } } } }
Upload/UploadHandler.cs(vb)
C#
using System; using System.Web; using System.Web.SessionState; namespace Upload { public class UploadHandler : IHttpHandler { public bool IsReusable { get { return true; } } public void ProcessRequest(HttpContext context) { SaveFilesHelper.Save(context); } } }
Upload/Web.config
Code
<?xml version="1.0" encoding="utf-8"?> <configuration> <system.web> <compilation debug="true" targetFramework="4.7.2" /> <httpRuntime maxRequestLength="4096" /> <httpHandlers> <add type="Upload.UploadHandler, Upload" path="Uploader.ashx" verb="POST" validate="false" /> </httpHandlers> </system.web> <system.webServer> <validation validateIntegratedModeConfiguration="false" /> <handlers> <add type="Upload.UploadHandler, Upload" path="Uploader.ashx" verb="POST" name="UploadHandler" preCondition="integratedMode" /> </handlers> <security> <requestFiltering> <requestLimits maxAllowedContentLength="30000000" /> </requestFiltering> </security> <httpProtocol> <customHeaders> <add name="Access-Control-Allow-Origin" value="*" /> <add name="Access-Control-Allow-Headers" value="Origin, X-Requested-Width, Content-Type, Accept" /> <add name="Access-Control-Allow-Methods" value="Get,PUT,POST,DELETE,OPTIONS" /> </customHeaders> </httpProtocol> </system.webServer> </configuration>

Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.