How to send querystring value via Ajax call to Handler page

peter liles 556 Reputation points
2022-09-19T16:48:58.917+00:00

I want to pass querystring value obtained from current page to Handler.ashx page via Ajax call using script below? At present it is passing nothing value to handler.
<script>
var counter;

     function UploadFile() {  

     
         var files = $("#<%=imageUpload.ClientID%>").get(0).files;  
        counter = 0;  
        // Loop through files  
        for (var i = 0; i < files.length ; i++) {  
            var file = files[i];  
            var formdata = new FormData();  
            formdata.append("file1", file);  
            var ajax = new XMLHttpRequest();  
       

            ajax.upload.addEventListener("progress", progressHandler, false);  
            ajax.addEventListener("load", completeHandler, false);  
            ajax.addEventListener("error", errorHandler, false);  
            ajax.addEventListener("abort", abortHandler, false);  
            **ajax.open("POST", "FileUploadHandler.ashx?ID='" + <%=Request.QueryString.Get("PK_ID")%> + "'" );**  
              
            ajax.send(formdata);  
           

Public Class FileUploadHandler : Implements IHttpHandler

Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest  
      
    Dim test As String = context.Request.Params("PK_ID")
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,248 questions
{count} votes

Accepted answer
  1. Lan Huang-MSFT 25,471 Reputation points Microsoft Vendor
    2022-09-20T06:45:21.617+00:00

    Hi @peter liles ,
    The first is the problem of the ID not matching the PK_ID parameters, and also to confirm whether <% = Request.QueryString.Get( " PK_ID " ) % > has a value
    I suggest you to break the point to check the problem, you can refer to my example below:

    Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest  
      
            context.Response.ContentType = "text/plain"  
            Dim file As String = context.Request.Params("ID")  
            context.Response.Write(file)  
      
        End Sub  
    

    Public PK_ID As String = "1"  
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load  
      
        End Sub  
    

    242832-image.png
    242843-test2.gif
    Best regards,
    Lan Huang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. peter liles 556 Reputation points
    2022-09-23T14:30:33.317+00:00

    After reading your example and testing i came to the understanding that my querystring was incorrect in the handler! i converted it to integer as i normal do. But in this instance it would not accept only in string format?
    I scrapped the idea of utilizing javascript to upload files. Mainly because a requirement was to also update images in gridview.
    Only reason i decided to try Javascript was because it came with a progress meter example and i discovered was not possible with a UpdatePanel approach?
    I stumbled across a solution to that problem using scriptmanager handler!

    0 comments No comments