File Loading from URLs

Hey, this is somethign that has been posted on the forums before but it would be pretty sweet to have added into the core.

Loading files from URLs is something I do pretty regularly, but it took me a while to get it set up and working well to a point that its simple and great. Right now I have a class that lets you load in anything from a url and either pass it to a string or save it to a file. I also have an extended version of ofImage that lets you load in images from a URL, which is super helpful. This uses the loading class I have.

This is the file loader class
http://www.vargatron.com/storage/ofxFileLoader.zip

The only caveat about it is that I’m using POCO and you can only do HTTPStreamFactory::registerFactory(); once so I have a global var in another H file that checks this. This class could probably be a singleton or there could be another good way to do this.

The ofImage extending class I have uses the following code

  
//------------------------------------------------------------------  
void ofxAdvancedImage::loadFromUrl(string _url) {  
	//Get the data  
	string str=loader.loadFromUrl(_url);  
	  
	int bytesToRead = str.size();  
	  
	unsigned char buff[bytesToRead];  
	memset(buff, 0, bytesToRead);  
	  
	for(int i = 0; i < bytesToRead; i++){  
		buff[i] = str[i];  
	}  
	  
	//Load the Image from the Stream  
	loadFromData(buff, bytesToRead);  
}  
  
  
  
//------------------------------------------------------------------  
void ofxAdvancedImage::loadFromUrl(string _url,string filename) {  
	loader.loadFromUrl(_url, filename);  
	  
	loadImage(filename);  
	  
	reset();  
}  

which uses the ofxFileLoader class to load files by either a string or savign to disk. This uses some of my own stuff but the basic idea is there.

Somethign along these lines would be awesome, its all based on code posted by Zach or Theo at one point and I’m sure it could be easily and nicely integrated into the core.

Thanks,
-Steve

Thanks for this - I am trying to load an image from a url but am running into issues with the

loadFromData - is this a part of ofImage I am missing?

is there anyway you could post your ofxAdvancedImage class?

Sure here is the latest version, with error catching.

-Steve

ofxFileLoader.zip

revisiting this with a better understanding in case it helps anyone else

  
  
void testApp::setup(){	  
	ofxFileLoader loader;  
	ofImage orgImg;  
	if(loader.loadFromUrl("[http://chart.apis.google.com/chart?chs=500x500&cht=qr&chl=http://code.google.com/apis/chart/image-charts.html",](http://chart.apis.google.com/chart?chs=500x500&cht=qr&chl=http://code.google.com/apis/chart/image-charts.html",) "bob.png"))  
	{  
		cout << "got it" <<endl;  
		  
		orgImg.loadImage("bob.png");  
	}else {  
		cout << "some error" <<endl;  
	}  
}  
  
  

Hi stevevarga…

the ofxfileLoader is really cool! I am having a issue though, i am using this class for the iphone and it works fine on the simulator, but not on the device.

I get the few error messages on the debug window :
Error opening file: -43
Error opening file: -43
Error getting file format: -50
Error getting file data size: -50
Error loading file info: -50
Error reading file data: -50
Error attaching data to buffer
: a003
Error loading looping file info: 40963

any idea if im doing something wrong? Like i said… on the simulator there are no issues!

thanks in advance.
adi