I'm trying transmit an image file from the server to the client, but my javascript callback becomes active before the stream closes I doing this because sending it in a traditional render json: times out and takes way to long anyway. The stream takes much less time, but i keep can't get all the data before the callback fires up.

controller code

   def mytest
    image=ImageList.new(AssistMe.get_url(image_url))
    response.stream.write image.export_pixels(0, 0, image.columns, image.rows, 'RGBA').to_s
    response.stream.close
   end

javascript

 var getStream, runTest;

 runTest = function() {
 return $.post('/dotest', getStream);};

 getStream = function(params) {
    return document.getElementById('whatsup2').innerHTML = 
  "stream is here " +     params.length;};

the response is an array, I can make it an array of arrays by adding a "[" at the front and a "],['finish'] at the end to be able to detect the end of the data, but I haven't been able to figure out how to get javascript to wait until the end of stream to run. I assume i need to set up some kind of pole to check for the end, but how do I attach it to the callback?

有帮助吗?

解决方案

Okay, here's a blog that describes this pretty well

blog

But i decided to forgo a stream and use .to_s. Since you can pipe several actions tougher

render object.method.method.to_s you get all the server side benefits of using a stream without the complexity. If you have a slow process where you need to overlap the client and server actions, then go to the blog and do it. Otherwise to_s covers it pretty well

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top