Category / Section
Add request header for upload action
1 min read
Description:
To add a request header during upload action in Upload box.
Solution:
You can add a custom request reader in upload box action during beforeSend event of Uploadbox which will be triggered before file has been sent to the server.
Please refer to the below code:
<script type="text/javascript"> $(function () { // declaration $("#UploadDefault").ejUploadbox({ saveUrl: "api/UploadBox", beforeSend: function (args) { args.xhr.setRequestHeader("securityToken", "23456"); } }); }); </script>
public class UploadBoxController : ApiController { [AcceptVerbs("Post")] public void Save() { if (HttpContext.Current.Request.Files.AllKeys.Any()) { var httpPostedFile = HttpContext.Current.Request.Files["Uploadbox"]; if (httpPostedFile != null) { var fileSave = HttpContext.Current.Server.MapPath("UploadedFiles"); if (!Directory.Exists(fileSave)) { Directory.CreateDirectory(fileSave); } var fileSavePath = Path.Combine(fileSave, httpPostedFile.FileName); httpPostedFile.SaveAs(fileSavePath); } } } }
You can get the header details from HttpContext.Current.Request.Headers. Security token details can be viewed in network tab as shown in the below image.
Sample: Uploadheader399918897