Files to look at:
- Default.aspx (VB: Default.aspx)
- Default.aspx.cs (VB: Default.aspx.vb)
- SaveFilesHelper.cs (VB: SaveFilesHelper.vb)
- UploadHandler.cs (VB: UploadHandler.vb)
- Web.config (VB: Web.config)
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:
- 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
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>
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);
}
}
}
}
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);
}
}
}
}
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>