AS3. SecurityError: Error #2000 when simply load external jpg
When I try to load an external image开发者_Python百科 in the same folder with a Loader object, I receive the error SecurityError: Error #2000: No active security context
.
var urlImg = String("img.jpg");
var imgLoader:Loader = new Loader();
var _lInfo:LoaderInfo = imgLoader.contentLoaderInfo;
_lInfo.addEventListener(Event.COMPLETE, handleComplete);
imgLoader.load(new URLRequest(urlImg));
function handleComplete(evt:Event):void{
trace("handleComplete");
}
Why doesn't it work?
Try changing your call to load:
imgLoader.load(new URLRequest(urlImg));
To do this instead:
var context:LoaderContext = new LoaderContext();
context.applicationDomain = ApplicationDomain.currentDomain;
imgLoader.load(new URLRequest(urlImg), context);
Otherwise you can try changing the use-network property when you compile with mxmlc. In Flash Pro, under publish settings, look for "Local playback security: " and toggle between 'Access local files only' or 'Access network only'.
Also don't forget to verify that your image is actually in the correct folder and named exactly "img.jpg" and not "image.jpg" or "img.JPG" or something like that.
The SecurityError
exception is thrown when some type of security violation takes place.
Examples of security errors:
- An unauthorized property access or method call is made across a security sandbox boundary.
- An attempt was made to access a URL not permitted by the security sandbox.
- A socket connection was attempted to an unauthorized port number, e.g. a port above 65535.
- An attempt was made to access the user’s camera or microphone, and the request to access the device was denied by the user.
From : http://www.actionscripterrors.com/?p=409
Can you try with this code :
var loader:Loader = new Loader();
var context:LoaderContext = new LoaderContext();
context.checkPolicyFile = false;
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);
loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, onError);
var request:URLRequest = new URLRequest(imageUrl);
loader.load(request, context);
var urlImg = String("img.jpg"); var imgLoader:Loader = new Loader(); var _lInfo:LoaderInfo = imgLoader.contentLoaderInfo; _lInfo.addEventListener(Event.COMPLETE, handleComplete); imgLoader.load(new URLRequest(urlImg)); function handleComplete(evt:Event):void { trace("handleComplete"); var mc:MovieClip = new MovieClip(); addChild(mc); mc.addChild(imgLoader.content); }
its working fine.
I think url location may be wrong. and use IOErrorEvent class and find out the error.
Assuming the code you posted is not the code you are actually using.
I have run into this issue and have noticed a bug with concatenating URLs on the same line you assign the source.
// bad code
imageObj.source = partialURL1 + partialURL2 + partialURL3;
//good code
var someURL:String = partialURL1 + partialURL2 + partialURL3
imageObj.source = someURL;
Actually it works for me. I got only a warning in the first line.
You got:
var urlImg = String("img.jpg")
But should be replaced to:
var urlImg:String = "img.jpg";
Also make sure that file is in the same directory so your SWF.
Also be sure to watch your addressing rules. addresses that work in html code may not work in actionsrcipt. Especially where subfolders are concerned. for some reason, this is often reported as a security error instead of URL not found error.
精彩评论