5

I have an HTML page that upload a fileOfRecords.txt containing item to add to a database (.NET/C# server). Upload the file work fine.

I'm trying to have the server send a status back to the client as each line in the file is being processed.

The controller method will loop thru every row adding the items to the database. On every iteration I would like to send a message to the client with a status whether the it succeeded of failed. Can anyone guide me as to how this is done using jQuery, AJAX, and C#?

Unfortunately I have no code since I have absolutely no idea how to start.

Andre Rubnikowich
  • 419
  • 1
  • 3
  • 10
  • So which are you trying to do? Upload a file to the server, or are you trying to send a status as each line is being processed? Or both. You should probably just send the file using a file select. Then return the status as its being processed, streaming the file and processing it at the same time seems a bit bizarre but it can be done. – johnny 5 Dec 15 '15 at 16:07
  • also, why are you trying to do this? – johnny 5 Dec 15 '15 at 16:08
  • 3
    In normal situations, the server cannot "push" data to the client. That is a limitation of the HTTP protocol. The client can repeatedly request data, and the server can respond with data when it's available. That's called polling. If you do this often enough, it can give the appearance that the server is pushing data to the client. You can achieve actual pushing of data using technology such as [web sockets](https://en.wikipedia.org/wiki/WebSocket), commonly implemented in .NET with [SignalR](http://signalr.net/). – mason Dec 15 '15 at 16:10
  • SignalR. That's the route you should be taking. – ragerory Dec 15 '15 at 18:47

2 Answers2

1

Are you trying to get jquery to call a server side function?

well, good news.

http://encosia.com/using-jquery-to-directly-call-aspnet-ajax-page-methods/

David May
  • 368
  • 3
  • 10
-1

When your browser send files, they are compressed to save bandwidth and some checksums are being calculated to ensure the files arrive "safely" at destination. This mean that the server will only be able to process the request when all the data have been transmited.

What you can do however, is sending your file content as chunk (line by line or n-line by n-line) to a webservice, allowing the server to process it quickly and respond to your ajax handler. Depending on your application, like if the content of your file matter as a whole), you might need to create some sort of a transmission session structure to keep in sync the browser transmission and the server response (to ensure they are not jumbled up)

Remy Grandin
  • 1,638
  • 1
  • 14
  • 34
  • 1
    The compression has nothing to do with the server being unable to push the data. That limitation actually comes from the HTTP protocol itself. – mason Dec 15 '15 at 15:42
  • Indeed. I didn't said it's the root cause of the problem, but it's one of the most comprehensible reason. – Remy Grandin Dec 15 '15 at 15:49
  • Mentioning compression here is only going to confuse things. It is not the "most comprehensible" reason, just a red herring. – mason Dec 15 '15 at 15:56