Click here to Skip to main content
15,662,659 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
Hi there I am currently using a file uploader on my website to upload files to my database. Before the file is uploaded I need to carry out some validation that can only be done server side as it needs to access the database. The uploader is using XMLHttpRequest and my application uses ASP.Net web forms. The problem is that if the validation fails server side and an error is returned the response is not received client side. It works correctly if the file is uploaded or even if the validation is carried out after the file is uploaded. Anyway below is a simple example of what im doing.

var xhr = this._xhrs[id] = new XMLHttpRequest();

xhr.onreadystatechange = function () {
  if (xhr.readyState == 4) {
    self._onComplete(id, xhr);
};"POST", queryString, true);
xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest");
xhr.setRequestHeader("X-File-Name", encodeURIComponent(name));
xhr.setRequestHeader("Content-Type", "application/octet-stream");

Sub ProcessRequest(ByVal context As HttpContext) Implements HttpHandler.ProcessRequest
  Dim fileName As String = context.Request.QueryString("qqfile")

  if Not Database.IsValid(fileName) then
    context.Response.Write("{error:""Invalid file""}")
  end If

  Dim newFile As New FileStream(strPath + "\" + fileName, FileMode.Create)
  Dim body As Byte() = context.Request.BinaryRead(context.Request.TotalBytes)
  newFile.Write(body, 0, body.Length)
  context.Response.Write("{success: true}")

End Sub
Yuri Vital 26-Sep-11 9:30am    
You are listening readyState == 4 only ...
Have tried to see what happen on ReadyState =3 ?

Have you seen what append during the client/server communication, capturing packet with Fiddler ( ?
TimWatson123 26-Sep-11 9:46am    
I dont receive a readyState == 3 when returning the error before I have uploaded the file, only after I have uploaded the file. But I do receive a readyState == 1 and the status is [Exception: DOMException]. Any ideas why?
Yuri Vital 26-Sep-11 9:58am    
readyState == 1 mean "Connection Open" see here :

Have you tried to reply simply : {success: false}
TimWatson123 26-Sep-11 10:17am    
Yeah ive tried that, it didnt work.

If I call Request.TotalBytes before returning the error code it works. The only problem is that TotalBytes hangs until the full file has been uploaded to the server. For some reason I only receive readyStates 1 and 4 when returning before uploading. I think this is my main problem:

"responseText of type DOMString
If the readyState attribute has a value other than 3 (Receiving) or 4 (Loaded), it must be the empty string. Otherwise, it must be the the body of the data received so far, interpreted using the character encoding specified in the response, or UTF-8 if no character encoding was specified. Invalid bytes must be converted to U+FFFD REPLACEMENT CHARACTER."

Am I trying to set the response text when the readyState is not 3 or 4? If so is there a way of causing the readyState to change to 3 or 4 server side before setting the response text?

You may take help from following:

Lightweight Image Upload with Animation[^]
Share this answer
if you are on Windows you can post your XMLHTTPRequest in the Export Folder that's found in the Windows System32 directory under inserv....Every time the computer shuts down anything that is in that Export file is "picked up" and received by ...iis or winremotemgmt team using that folder as a back door to collect your collectibles...faxes, etc.
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900