open local html file with navigateToURL on android & ios - actionscript-3

I have created an HTML file and I want to show it with navigateToUrl method:
var htmlFile:File = File.documentsDirectory.resolvePath("WebWin//Factor//" + "Factor" + ".html");
var fs:FileStream = new FileStream();
fs.open(htmlFile, FileMode.WRITE);
fs.writeUTFBytes(htmlString);
fs.close();
if(htmlFile.exists)
{
navigateToURL(new URLRequest(htmlFile.url), "_blank");
}
but on Android or iOS I can't open HTML file (I don't have any issue on windows)
this is htmlFile.url :
file///var/mobile/Containers/Data/Application/AB199919-1E41-47E7-9716-DA5CD746D113/Documents/WebWin/Factor/Factor.html
Also for URLRequet url, I tried other ways like("http://"+htmlFile.url) but I wasn't successful.
Do me a big favor if you help me

Related

Animate 2021 Action Script 3 URLRequest causes Error 2035

I'm working on an application and we are using Flash to add an interface. During the initialize the app loads in a config file with references to image paths. When I use the Loader and URLRequest classes to load those images the path is not working and it's causing issues. Based on everything I've researched my code should work. I have a trace that outputs "source/icons/default.png" which is the correct relative path for the external image and loading the image works, but I need to use a variable to set the image path. Setting the URLRequest with a variable, new URLRequest("source/icons/" + default.png);, causes it use a full file path that looks wrong and reports a 2035 error. Is there something that I'm doing wrong? I'm not sure what to do at this point. I've verified the file exists and I've verified the path of the file.
Trace Ouput:
source/icons/default.png
Error Output:
[CompanionStatus] Error occurred while loading bitmap
[CompanionStatus] 2035 : Error #2035: URL Not Found. URL: file:///C|/Users/devtrooper/Desktop/project/source/icons/default.png
It looks like flash is replacing the colon (:) near the drive letter with a pipe (|) and I'm not sure if that is the issue.
Here is my method for loading images:
private function getBitmapData(nameString:String, imagePath:String):void{
try{
var bitmapDataFunction:Function = addBitmapDataToDictionary(nameString);
var path:String = ICON_FOLDER_PATH + imagePath;
var loader:Loader = new Loader();
var urlRequest:URLRequest = new URLRequest("source/icons/" + imagePath);
trace("URLRequest.url " + urlRequest.url);
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, bitmapDataFunction, false, 0, true);
loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, handleBitmapDataError, false, 0, true);
loader.load(urlRequest);
} catch (e:Error) {
log("Error occured while trying to load image data");
log(e.name + " : " + e.message);
}
}
you can use File method instead of urlRequest method like this:
var urlPath:String="";
var picFile:File = File.desktopDirectory.resolvePath("source/icons/default.png");//you can use applicationDirectory,applicationStorageDirectory,userDirectory,doumenntDirectory and ...
if (picFile.exists == true) {
urlPath = picFile.url;
}

chrome PDF viewer can't download file

Here is my situation, I got a server running a PDF generator, when I make a request with some params, it will give me back a PDF file, the PDF is not stored in the server it's generated during the runtime.
Everything goes fine, I can get the PDF open in chrome's PDF viewer, but if want to download the file, an error occurred, like the image shows.
Because Chrome go to the origin URL to request the file, but the file is not a static resource on the server.
I don't know if anybody has run into this problem?
Whenever you leave the website you used to create the object URL (window.URL.createObjectURL(...)) that very object will get garbage collected. So you need to keep a reference to that object somehow.
This works for us in Chrome, Firefox, Safari, iOS Safari & Android to first display the PDF in capable browsers in a new tab and allow a download afterwards (in IE it just starts the download):
function openPdfInNewTab(url,
postData,
description = 'Document',
filename = description + '.pdf') {
if (!window.navigator.msSaveOrOpenBlob) {
var tabWindow = window.open('', '_blank');
var a = tabWindow.document.createElement('a');
a.textContent = 'Loading ' + description + '..';
tabWindow.document.body.appendChild(a);
tabWindow.document.body.style.cursor = 'wait';
} else {
spinnerService.show('html5spinner');
}
$http.post(url, postData, {responseType: 'arraybuffer'})
.then(function showDocument(response) {
var file = new Blob([response.data], {type: 'application/pdf'});
if (window.navigator.msSaveOrOpenBlob) {
spinnerService.hide('html5spinner');
window.navigator.msSaveOrOpenBlob(file, filename);
} else {
tabWindow.document.body.style.cursor = 'auto';
var url = a.href = window.URL.createObjectURL(file);
a.click();
a.download = filename;
}
$timeout(function revokeUrl() {
window.URL.revokeObjectURL(url);
}, 3600000);
}, handleDownloadError);
}
We have been opening PDFs in a new browser-tab and had similar issues.
For us it started working again when we use window.URL.createObjectURL instead of tabWindow.URL.createObject which displayed the PDF but didn't allow the download.
Chrome's built in PDF viewer will download the pdf file through the PDF's origin URL. So if the PDF is generated at server runtime and if it's not stored in the sever, the download could fail.
see link here: https://productforums.google.com/forum/#!topic/chrome/YxyVToLN8ho
Just as an additional comment:
We had the same problem on a project, on Chrome only.
Authenticated GET request would fetch the PDF as an attachment from API, and we would forward it via window.createObjectURL(blob) to the browser viewer.
The Network Error was due us invoking window.revokeObjectURL(url); after opening the PDF. When we removed that line, the blob wasn't garbage collected immediately after opening.
fetch(request)
.then(async response => {
if (!response.ok) {
return reject(response.statusText);
}
const blob = await response.blob();
const url = await window.URL.createObjectURL(blob);
window.open(url, '_blank');
URL.revokeObjectURL(url); // This line was causing the problem
resolve();
})
.catch(reject)

File class AS3 / AIR: let user choose only save location, not file name or extension

When using browseForSave method, is it possible to let the user choose only the location of a file, and not the file name or the extension?
I'm creating an encrypted file, and I need its name and extensions not to be changed by the user.
Thx!
EDIT (AFTER SOLVED)
I was simply looking for the browseForDirectory method. Shame on me. :)
For a reference on how to open a browse dialog to choose a folder see the example here:
How to create "Browse for folder" dialog in Adobe FLEX?
once you have your directory you can piece that together with code here to save a file using the FileStream object:
http://blog.everythingflex.com/2008/02/25/file-and-filestream-within-air/
copied here since it's an external link
private function saveFile():void{
var myPattern:RegExp = / /g;
var newFileName:String = fileName.text.replace('.txt','');
if(newFileName.length > 1){
var file:File = File.desktopDirectory.resolvePath("Files/" + newFileName.replace(myPattern,'_') + ".txt");
var stream:FileStream = new FileStream()
stream.open(file, FileMode.WRITE);
var str:String = contents.text;
str = str.replace(/\r/g, File.lineEnding);
stream.writeUTFBytes(str);
stream.close();
fdg.directory = File.desktopDirectory.resolvePath("Files/");
fileName.text = "";
contents.text = "";
} else {
mx.controls.Alert.show("File name is required", "Error Saving File");
}
}
One solution would be to set the file object like this:
var f:File = File.desktopDirectory.resolvePath("*.txt");
f.addEventListener(Event.SELECT, onSelected);
f.browseForSave("save txt file");
Although user can still override that in the opened dialog, but at least the dialog shows the file extension. And later in the Event.SELECT you can check and confirm what user has selected.

Upload file to server without using FileReference.browse()

I am trying to upload file to server with the FileReference class.
the file is mp3 that was encoded in runtime without saving him on the local hard drive.
Is it possible to fit in the mp3Encoder into the FileReference somehow?
Thanks
Assuming you have the mp3 as a ByteArray (not sure how else it would exist at runtime if you haven't saved it to disk), you can use the URLLoader class, like this:
var scriptRequest:URLRequest = new URLRequest("PATH_TO_YOUR_UPLOAD_SCRIPT");
var scriptLoader:URLLoader = new URLLoader();
var scriptVars:URLVariables = new URLVariables();
scriptLoader.addEventListener(Event.COMPLETE, handleLoadSuccessful);
scriptLoader.addEventListener(IOErrorEvent.IO_ERROR, handleLoadError);
scriptVars.var1 = MP3_AS_BYTE_ARRAY;
scriptRequest.method = URLRequestMethod.POST;
scriptRequest.data = scriptVars;
scriptLoader.load(scriptRequest);
function handleLoadSuccessful($evt:Event):void
{
trace("Message sent.");
}
function handleLoadError($evt:IOErrorEvent):void
{
trace("Message failed.");
}
Then on the server, you just need to save the stream to a file and give it the correct mp3 mime type.
There's a decent tutorial, from which this example is adapted, here: http://evolve.reintroducing.com/2008/01/27/as2-to-as3/as2-%E2%86%92-as3-loadvars-as3-equivalent/
And if you need to send your request as multipart/form-data (so it appears to be an attached file), there's a good example of this here: http://marstonstudio.com/2007/10/19/how-to-take-a-snapshot-of-a-flash-movie-and-automatically-upload-the-jpg-to-a-server-in-three-easy-steps/

Adobe AIR and different OS filesystems

Another Adobe Air question for you but first here some background into the project I have been tasked with. It is an AIR app that will read assets from a USB key and must work on both WIN and MacOS. The problem is, how do I load assets into the app on MacOS! Sounds simple enough and works seamlessly on Windows.
Here is a code snippet of what i am trying to do:
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, ok);
loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, ioError);
var p:String;
if (os == "mac")
{
p = "/Volumes/" + keyVolume.rootDirectory.name + File.separator + "0a0ff0ff-f7ae-4b9c-9637-843b1d6c80e8.jpg";
}
else
{
p = keyVolume.rootDirectory.name + File.separator + "0a0ff0ff-f7ae-4b9c-9637-843b1d6c80e8.jpg";
}
var temp:File = new File(p);
Debugger.Display.text += "\nAttempting to load: " + p;
Debugger.Display.text += "\nDoes it exist? " + temp.exists;
loader.load(new URLRequest(p));
... the variable OS and keyVolume are being successfully set in earlier code. Also, I have the event listener callbacks defined as well for ok() and ioErro().
When this is run it prints out on windows:
Attempting to load: G:\0a0ff0ff-f7ae-4b9c-9637-843b1d6c80e8.jpg
Does it exist: true
... and then successfully loads the asset.
On MacOS, it prints out:
Attempting to load: /Volumes/AC/0a0ff0ff-f7ae-4b9c-9637-843b1d6c80e8.jpg
Does it exist: true
... and then fails with an IOError every time.
Can anyone see something that I am missing here? Do I have some sort of permission error or something (file has "read / write" access). The USB key is formatted in MS-DOS FAT32, could that be a problem?
EDIT
I formatted a new USB key in MacOS to FAT16 and put the files onto it with no success. Problems remain.
EDIT
I am now just trying to load an asset from /users/-USERNAME-/Desktop and still am receiving the same error, so it looks like it isn't a permissions issue on just the USB stick, it is more widespread than that.
EDIT
PROBLEM SOLVED! I finally worded my Google search correctly and it revealed the answer.
These changes will fix the problem:
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, ok);
loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, ioError);
var p:String = keyVolume.rootDirectory.nativePath + ((os == "mac") ? File.separator : "") + "0a0ff0ff-f7ae-4b9c-9637-843b1d6c80e8.jpg";
var temp:File = new File(p);
Debugger.Display.text += "\nAttempting to load: " + temp.url;
Debugger.Display.text += "\nDoes it exist? " + temp.exists;
loader.load(new URLRequest(temp.url));
I have also refined the logical statement involving the OS detection a bit as well.
I hope someone finds this useful!
PROBLEM SOLVED! I finally worded my Google search correctly and it revealed the answer.
These changes will fix the problem:
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, ok);
loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, ioError);
var p:String = keyVolume.rootDirectory.nativePath + ((os == "mac") ? File.separator : "") + "0a0ff0ff-f7ae-4b9c-9637-843b1d6c80e8.jpg";
var temp:File = new File(p);
Debugger.Display.text += "\nAttempting to load: " + temp.url;
Debugger.Display.text += "\nDoes it exist? " + temp.exists;
loader.load(new URLRequest(temp.url));
I have also refined the logical statement involving the OS detection a bit as well.
I hope someone finds this useful!