Question

My website allows users to add text to an image, and displays the result to them via an Ajax.request. Sometimes it works great, but other times the image is incomplete and javascript records the error "Image corrupt or truncated."

How can I make sure the file is completely written before sending the Ajax response back to the browser?

VIEW
  new Ajax.Request('<%= url_for(:action => "update_image", :id => @user_image.id) %>?greeting=' + encodeURIComponent(elmgreeting.value), { onSuccess: document.getElementById("card_image").src=card_filename });

MODEL
  def create_card
    img = Magick::Image.read(self.input_image).first
    # ... add the greeting to the image
    img.write(self.card_filename)
    self.card_width = img.columns
    self.card_height = img.rows
    self.card_size = img.filesize
  end

CONTROLLER
  def update_image
    @user_image = UserImage.find(params[:id])
    @user_image.greeting = params[:greeting]
    @user_image.create_card
    @user_image.save
    render :layout => false
  end

[I've also noticed the img.filesize value assigned to the card_size is often inaccurate, leading me to think it's being obtained too early, as well.)

I've tried everything I could find on stackoverflow and elsewhere, but just can't figure it out. Any help greatly appreciated.

FOUND MY PROBLEM: It was the Ajax request. It now works nicely:

new Ajax.Request('<%= url_for(:action => "update_image", :id => @user_image.id) %>?greeting=' + encodeURIComponent(elmgreeting.value), { onComplete:function(request){document.getElementById("card_image").src=card_filename}});

No correct solution

OTHER TIPS

afaik the things you write should be already synchronous, there is probably something else that breaks things. what did your rails server log said? maybe there is an error happens and throws some errors and then your front-side tries to use it as an image source

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top