Get a value from a textbox when uploading

Hello,

How do I get a value from a textbox " DescricaoGestor" and pass to controller when uploading?

Thanks


 <div class="form-group col-md-8 textbox-row">
                <ejs-textbox id="DescricaoGestor" floatLabelType=Always placeholder="Descrição"></ejs-textbox>
 </div>

 <div class="form-group col-md-4 textbox-row">
                @{                    
                    var asyncSettings = new Syncfusion.EJ2.Inputs.UploaderAsyncSettings { SaveUrl = "/OrdemServico/UploadAtividadeInterna/" + Model.Id };
                }
                <div class="button-containerdetails" style="margin-bottom:20px;">
                    <div class="button-left" style="margin-right:30px;">
                        <ejs-uploader id="uploadFiles" locale="pt" allowedExtensions=".*"
                          asyncSettings="@asyncSettings" autoUpload="false" Success="SuccessHandler"></ejs-uploader>
                    </div>
                </div>
            </div>


public void UploadAtividadeInterna(IList<IFormFile> UploadFiles, long id, string DescricaoGestor)
        {
            try
            {
                foreach (var file in UploadFiles)
                {   
                    if (file != null)
                    {
                                    
                    }
                }
            }
            catch (Exception e)
            {
                Response.Clear();
                Response.StatusCode = 204;
                Response.HttpContext.Features.Get<IHttpResponseFeature>().ReasonPhrase = "Falha no upload";
                Response.HttpContext.Features.Get<IHttpResponseFeature>().ReasonPhrase = e.Message;
            }
        }

2 Replies 1 reply marked as answer

YS Yohapuja Selvakumaran Syncfusion Team February 19, 2024 01:52 PM UTC

Hi Fernando,


Thank you for reaching out to us. We have created a sample based on your requirements, which involves passing the textbox value to the server using the setRequestHeader method in the uploading event, and then retrieving this value in the server-side controller.


Below is the code snippet for your reference:


 

[index.cshtml]

<ejs-uploader id="UploadFiles" asyncSettings="@asyncSettings" removing="onFileRemove" uploading="uploadStart"></ejs-uploader>
<ejs-textbox id="textbox"></ejs-textbox>


<script>
    function onFileRemove(args) {
        args.postRawFile = false;
    }
     function uploadStart(args) {
        var accessToken = document.getElementById("textbox").value;
        args.currentRequest.setRequestHeader('Authorization', accessToken);

    }

</script>

 

[UploaderController.cs]

 

        public void Save(IList<IFormFile> UploadFiles)
        {
            try
            {
                var saveLocation = @"D:\"; // replace with the path where you want to save the files
                var authorization = HttpContext.Request.Headers["Authorization"];    // you can get the textbox value in this authorization

               ……

 

 



This code ensures that the value entered into the textbox is passed to the server-side controller through the Authorization header in the HTTP request. You can then retrieve this value in the controller using HttpContext.Request.Headers["Authorization"].



Regards,

Yohapuja S


Attachment: SyncfusionCoreUploader_token_textbox_1aa1bf00.zip

Marked as answer

FE Fernando February 23, 2024 04:58 PM UTC

Hi, it worked perfectly. Thank you very much!


Loader.
Up arrow icon