AS3: Download a download embedded file to desktop without browser - actionscript-3

I'm creating an flash app where users can select something and download a temple. I'm publishing this file using air application with runtime embedded. In the app I've included a folder called documents with the individual files the user can download. Currently I'm using navigateToURL but I don't want it to rely on the browser. I've also tried this:
function surveyDownload(evt:MouseEvent):void {
var request = new URLRequest("document/template.docx");
var localRef = new FileReference();
try
{
// Prompt and download file
localRef.download( request );
}
catch (error:Error)
{
trace("Unable to download file.");
}
}
but all I get is the trace statement "Unable to download file".
How can I download an embedded file without the browser?

Your question is a little unclear. You want the user to download something from a server? If so then document/template.docx is not a URL so of course that will not work.
If you are talking about copying a file out of the AIR app bundle to the user's hard drive then you don't need URLRequest but rather the methods in the File class (browseForSave and copyTo).
Read the docs for File and search out some tutorials – there are loads and more complete than I would write here.

Related

Create download link for static asset file

I would like to put a download link on my website so the users can download the file template.csv. The file is static and is located at the root of my /grails-app/assets folder.
Inside my page.gsp, I have tried 2 methods do so, to no avail :
Download the template
this results in a template.csv file being downloaded, but the content is the html code of page.gsp rather than the original content of the file I uploaded in my assets.
Download the template
the generated html file has a link to localhost:8080/mysite/assets/template.csv but clicking it prompts an error message : Failed - no file.
What is the correct way to do what I want to achieve ? Is there an issue with extra permissions I would need to add to allow the download of my file ?
Our webapp relies on a rather old technological stack :
Grails 2.3.4
Plugin asset-pipeline-2.2.5
I have had some troubles with downloading files straight with a -tag.
To overcome this, I use Controller method to return the file and place the downloadable files in their own folder under web-app/:
class MyController {
#Autowired()
AssetResourceLocator assetResourceLocator
def downloadExcelTemplate () {
String fileName = "MyExcelFile.xlsx"
/* Note: these files are found in /web-app/downloadable directory */
Resource resource = assetResourceLocator.findResourceForURI("/downloadable/$fileName")
response.setHeader "Content-disposition", "attachment; filename=${resource.filename}"
response.contentType = 'application/vnd.ms-excel'
response.outputStream << resource.inputStream.bytes
}
}
And just use regular a-tag to to link to this controller method.
This way you also gain more control over file downloads.

UWP - How to read .jpg from Desktop inside of app

The functionality I am needing for my app is that if a user copies an image file (for example, a .jpg file in their Desktop folder), I need to extract that file from the clipboard and display it in my app.
I can already extract the storage file from the clipboard, and get the file path. But when I try to read the file path into a BitmapImage, I received a System.UnauthorizedAccessException error, because the image is not located in the app's folder.
It sounds like you are trying to open the file by path rather than directly using the StorageFile returned by the file picker.
Your app doesn't have direct access to most of the file system (including the downloads directories) and can access such files only indirectly via the file system broker. The StorageFile object works with the broker to open files the use had authorized and provides a stream of the files contents for the app to read and write to.
You can use Xamarin.Plugin.FilePicker or FilePicker-Plugin-for-Xamarin-and-Windows .
You may x:Bind path to XAML Image
<Image>
<Image.Source>
<BitmapImage UriSource="{x:Bind GetAbsoluteUri(Path)}"/>
</Image.Source>
</Image>
Code behind:
private Uri GetAbsoluteUri(string path)
{
return new Uri(Path.GetFullPath(path), UriKind.Absolute);
}
Try, hope it'll work :)

How can I run a command in windows store app?

I need to run a command from windows store app?
the command is something like this : java -jar abc.jar
How can I do that?
EDIT :
I tried this but with no luck. It says file not found.
string exeFile = #"C:\DATA\start.bat";
var file = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFileAsync(exeFile);
if (file != null)
{
// Set the option to show the picker
var options = new Windows.System.LauncherOptions();
options.DisplayApplicationPicker = true;
// Launch the retrieved file
bool success = await Windows.System.Launcher.LaunchFileAsync(file, options);
if (success)
{
// File launched
}
else
{
// File launch failed
}
}
The app container blocks this behavior for Store apps.
First of all, you're attempting to obtain a StorageFile through your package InstalledLocation folder, which will not work. InstalledLocation is a StorageFolder, and its GetFileAsync looks for files only within that immediate folder. This is why it's returning file not found.
The API that takes an arbitrary path is Windows.Storage.StorageFolder.GetFileFromPathAsync. However, your ability to access files is limited by the app container. You can access files in your package folder or app data locations by default, or the various media libraries if you've declared access in the manifest, but otherwise you have to go through the file picker so the user is aware of what you're doing and can grant consent. Simply said, this is the only way you'll get to a file in a location like c:\data. You can play with this using Scenario 1 of the Association launching sample and the "Pick and Launch" button.
If you can get that access permission, then in you'll be able to launch a file if it's not a blocked file type. Data files (like a .docx) that are associated with another app work just fine, but executables are wholly blocked for what should be obvious security reasons. You can try it with the sample I linked to above--pick .bat, .cmd, .exe, .msi, etc. and you'll see that LaunchFileAsync fails.
Also note that the other launcher function, LaunchUriAsync, also blocks file:/// for the same reasons.

Windowsphone - How to Make App can open Some Fileformats

i have a question
i want to know how can i open downloaded file in my app (like when you download .PDF File in Internet Explorer it shows apps that can open .PDF files )
please help me snippet of codes !
first you have to save your file in isolated storage and you need to use launcher this code would help you:
private async void Button_Click_1(object sender, RoutedEventArgs e)
{
StorageFolder local = Windows.Storage.ApplicationData.Current.LocalFolder;
// Access the bug query file.
StorageFile yourfile = await local.GetFileAsync("Pradeep.pdf");
// Launch the bug query file.
Windows.System.Launcher.LaunchFileAsync(yourfile);
}
this launcher is available in windows phone 8 only.
As stated in MSDN :
"You can use file and URI associations in Windows Phone 8 to automatically launch your app when another app launches a specific file type or URI scheme. When launched, a deep link URI is used to send the file (a reference to the file) or URI to your app."
Basically you need to do following steps :
Register your application to be associated to a specific file type, for example .pdf file extension in this case. This step done by adding <Extension> in WMAppManifest.xml.
When your application get launched upon user opening a .pdf file, get file id from query string then get the physical file using SharedStorageAccessManager based on file id.
The rest is to handle opening the file in your application.
I found this very nice blog explaining the detail of every steps, accompanied with downloadable source code.
PART I: explaining background concept and creating application to launch associated file
PART II: explaining details of step 1-3 above with sample application

How to store and use a native executable in a SWC with AIR?

I am developing an AIR application. This application needs some hardware accesses that are not possible with AIR. I decided to use the NativeApplication class in AIR, which launches a C# executable. The AIR application and the "native" application then communicate with the standard output and standard input streams.
A bit like that:
private var np:NativeProcess = new NativeProcess();
private var npi:NativeProcessStartupInfo = new NativeProcessStartupInfo();
private var args:Vector.<String> = new Vector.<String>();
private function creationCompleteHandler(event:FlexEvent):void {
args.push("myCommand");
args.push("myParameter");
npi.arguments = args;
npi.executable = File.applicationDirectory.resolvePath("MyNativeExe.exe");
np.addEventListener(ProgressEvent.STANDARD_OUTPUT_DATA, onData);
np.start(npi);
}
private function onData(e:ProgressEvent):void {
while(np.standardOutput.bytesAvailable != 0) {
trace(String.fromCharCode(np.standardOutput.readByte()));
}
}
I put MyNativeExe.exe file in the application directory, set the "extendedDesktop" value in the *-app.xml supportedProfiles, and it works fine.
Now, I would like to create a kind of AS3 SWC library that embeds MyNativeExe.exe and which provide an AS3 class to manage the interaction with MyNativeExe.exe. Therefore I could easily reuse this work in other AIR projects by simply addind the SWC file as a library. I may have to manually add the "extendedDesktop" value to the new AIR projects, but it is not a problem.
And I am stuck. I can embed an EXE file in a SWC file, by manually selecting the resources to embed with Flash Builder but...
it will not be automatically embeded in the final SWF file as only the needed parts of the SWC file are merged with the SWF
even if it is (enforcing the merge with an [Embed] tag, ...), how can I access and execute the embedded EXE file? NativeProcessInfo.executable needs a File object, and not a byte stream.
The only idea I have would be to embed the EXE file with [Embed], load it as a byte array, create a new file with the byte array as data, and then execute the file. I don't know if it works, but I do not like the idea, as it implies having the EXE kind of duplicated.
Does someone have an idea?
Thank you!
You should look into the Air Native Extensions. Simply put, one of the a new features in Air 3.0 was the ability to compile and link to custom language extensions directly from air. I haven't found an example of using C# directly, but there is a link on that page to doing with managed C++.