Forum: Ruby on Rails xml file download prompt instead of displaying in browser?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
A2d790f562e9381e8a39ab005a9d1582?d=identicon&s=25 Nik (Guest)
on 2008-10-14 01:55
(Received via mailing list)
Hello all,

I am trying to export some xml files on the web app as a download, but
Firefox/chrome/ie/safari all just display the xml content on
themselves. How can I have the download prompt? Like I am downloading
a .exe file?

Thank You
41cc2c53544016a86472343ab3fdc0ff?d=identicon&s=25 Simon Macneall (Guest)
on 2008-10-14 02:12
(Received via mailing list)
Hi Nik,

This is what we have and it seems to work in all the browsers

     response.body = xml.to_s
     response.content_type = "application/x-generated-xml-backup";
     filename = "Filename.xml"
     response.headers['Content-disposition'] = "Attachment;
filename=\"#{filename}\""
     render :text => xml.to_s

Cheers
Simon
A2d790f562e9381e8a39ab005a9d1582?d=identicon&s=25 Nik (Guest)
on 2008-10-17 01:00
(Received via mailing list)
Thanks Simon,

Where should I put this code? I have in my controller:

#Controller

def exportxml
    if params[:item_ids]
        @items = Item.find(params[:item_ids])

        respond_to do |format|
        format.xml {render :layout=>false}
        end
    end
end

Cheers!
Nik
286dec2cb2280805db4cb58c23463354?d=identicon&s=25 zuo peng (Guest)
on 2008-10-17 05:10
(Received via mailing list)
def exportxml
         if params[:item_ids]
       @items = Item.find(params[:item_ids])

       respond_to do |format|
         format.xml {
             send_data @items.to_xml, :filename => "items.xml"
         }
       end
   end
end


On Fri, Oct 17, 2008 at 6:59 AM, Nik <NiKSOsf@gmail.com> wrote:
>
>>
>> Simon
>> > Thank You
> >
>



--

Satchel Paige  - "Don't look back. Something might be gaining on you."
A2d790f562e9381e8a39ab005a9d1582?d=identicon&s=25 Nik (Guest)
on 2008-10-17 05:26
(Received via mailing list)
Hi Zuo,

Thank You! It works. Brilliantly. And thank you, too Simon!

All the best,
Nik
A2d790f562e9381e8a39ab005a9d1582?d=identicon&s=25 Nik (Guest)
on 2008-10-27 18:16
(Received via mailing list)
Hi Zuo,

It works! But the XML structure I used in the builder is now lost. The
XML file is now filled with the data from the columns.

Is there a way I can use my builder and your method?

Does anyone have an idea on this ?

Thanks!
Nik
286dec2cb2280805db4cb58c23463354?d=identicon&s=25 zuo peng (Guest)
on 2008-10-28 03:39
(Received via mailing list)
overwrite the to_xml method in your model or yield the builder object
as part of the to_xml

@item.to_xml do |xml|
    # builder stuffs
end


On Tue, Oct 28, 2008 at 1:15 AM, Nik <NiKSOsf@gmail.com> wrote:
> Thanks!
>> On Oct 16, 6:47 pm, "zuo peng" <cn.peng....@gmail.com> wrote:
>> >    end
>> > > def exportxml
>> > > Nik
>> > >>      render :text => xml.to_s
>> > >> > themselves. How can I have the download prompt? Like I am downloading
>> > >> > a .exe file?
>>
>> > >> > Thank You
>>
>> > --
>>
>> > Satchel Paige  - "Don't look back. Something might be gaining on you."
> >
>



--

Emo Philips  - "My computer beat me at checkers, but I sure beat it at
kickboxing."
A2d790f562e9381e8a39ab005a9d1582?d=identicon&s=25 Nik (Guest)
on 2008-10-30 21:00
(Received via mailing list)
Hi Zuo!

Thank you again for your help! I did this and it says that a "}" is
missing right after * as in :filename=>*

def exportxml
    if params[:item_ids] ||= []
        @items = Item.find(params[:item_ids])
        respond_to do |format|
          format.xml {send_data @items.to_xml do |xml|
           xml.instruct!
              xml.product do
              for t in @items
                xml.lineitem(:id=>t.name) do
                  xml.name(t.name)
                  xml.in(t.tradeIn/85.to_i)
                  end
                  xml.logginginfo do
                    xml.lognote(t.note)
                  end
                end
              end
            end
          end
        :filename=> "items.xml"}
        end
    end
  end

The builder stuff is copied straight from the exportxml.xml.builder.

And I don't know if this is helpful -- if I removed
the :filename=>"items.xml" from above, it downloads! Yet, it again
saves the file without extension and gives me the array of the columns
of the table instead of my builder scheme.

I am sorry to have to ask for your help again.

Thank you
This topic is locked and can not be replied to.