Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

force download in jquery loaded div [ codeigniter ]

Posted on 2012-04-05
5
1,676 Views
Last Modified: 2012-04-19
Hello all,

I have this codeigniter question.

I have created a force download link and this works perfectly. It is a controller with the method 'download' This gets some data and load the view that actually forces the download.

If I load this view (with jquery) into a div the raw data of the file is displayed, and there is no download.

Does anybody know why this is? I need to load it with jquery, do some other tasks and then force the download.

Hope someone can help me.


controller:
class Kleurplaten extends CI_Controller {

	public function __construct(){
            parent::__construct();
            $this->load->model('kleurplaten_model');
	}
        
        public function download_kleurplaat($id){
            
            // load data
            $content_data['kleurplaat'] = $this->kleurplaten_model->get_single_coloring_page($id);
            
            if (empty($content_data['kleurplaat'])){
                    show_404();
            }
            
            $this->load->view('pages/kleurplaten_download', $content_data);
        }

}

Open in new window


view:

$this->load->helper('download');

// do some fancy stuff here...

$data = file_get_contents(base_url().'images_upload/'.$kleurplaat['colpic_asset_location']); // Read the file's contents
$name = 'myphoto.jpg';

force_download($name, $data);

Open in new window

0
Comment
Question by:wal_toor
  • 3
  • 2
5 Comments
 
LVL 1

Expert Comment

by:frisoft
ID: 37810200
Hello,
what are you exactly (and how) loading into a div? Can you post a code related to this too?
Which file is displayed in the div? The view? Or the image?
Thanks,
Feri
0
 
LVL 8

Author Comment

by:wal_toor
ID: 37810229
Hello feri,

I am using jquery to load the data into a div:

$(".kleurplaat_downloadlink").click(function(event){
        
        event.preventDefault();
        
        var content_id = $(this).attr('rel'); // id of content is a rel attribute
        var content_holder = $(this).parent().find(".kleurplaat_download");
        var content_to_load = siteroot+'/kleurplaten/download/'+content_id;
        
        // load and process

       content_holder.load(content_to_load, function(response, status, xhr) {
            if (status == "error") {
                //error, do stuff
            }else if(status == "success"){
                //succes, do stuff
            }
        });

        
        
    });

Open in new window


It works, but de loaded view (where the download is initiated) shows only raw image data, no download.

But when I addres the url like www.site.nl/kleurplaten/download_kleurplaat/9 is works perfect

kleurplaten - controller
download_kleurplaat - method
9 - argument (id)

greetz,
walter
0
 
LVL 1

Expert Comment

by:frisoft
ID: 37810247
Hm, try to replace the call of content_holder.load(...) (the whole block) with this:
location.href = content_to_load;
0
 
LVL 1

Accepted Solution

by:
frisoft earned 500 total points
ID: 37810266
To clarify things, if You use jquery's load function, it's just a shortcut to the $.ajax method, it gets some content with ajax and replaces the given DOM node with this content, in Your case by an image (i guess).
Force download works only in the browsers, javascript has no (easy) way to save content to the user's disk, it's for security reasons.
0
 
LVL 8

Author Closing Comment

by:wal_toor
ID: 37864634
Hi Frisoft, Ahaa... this seemed to be the issue. Thanks for pointing this to me. I have found another solution to get this working.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question