question

peterliles-4155 avatar image
0 Votes"
peterliles-4155 asked peterliles-4155 commented

How to send querystring value via Ajax call to Handler page

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")
dotnet-aspnet-webpages
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

I'm guessing the ** is the line of code you're asking about????

The parameter name in the JavaScript code is "ID" but the handler is looking for PK_ID.

There's other issues with your code that you'll need to work through. Anyway, please learn how to use the browser's dev tool and the Visual Studio debugger to troubleshoot your code.

1 Vote 1 ·

i should look more into debugging i suppose though i find it best to pick up knowledge as i progress. It may be many months before i need to test using the browser and simply forget what i read about, if i don't constantly practice and time is of the essence. I dont seem to get anywhere through practice only experience.
Thats how it is!

0 Votes 0 ·
LanHuang-MSFT avatar image
0 Votes"
LanHuang-MSFT answered

Hi @peterliles-4155,
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.



image.png (43.4 KiB)
test2.gif (142.1 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

peterliles-4155 avatar image
0 Votes"
peterliles-4155 answered

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!

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.