Storing/Saving XML in local asset folder in AIR - air

AIR in general seems to be storing the xml file in the path where the app is installed.
I am generating an xml and I want to save/store a xml file in local asset folder of AIR application.
Any thoughts on doing this.

The File class has some static variables that point to local directories:
From the docs:
File.applicationStorageDirectory — a storage directory unique to each installed AIR application
File.applicationDirectory — the read-only directory where the application is installed (along with any installed assets)
File.desktopDirectory — the user's desktop directory
File.documentsDirectory — the user's documents directory
File.userDirectory — the user directory
Creating a pointer to the file
Usually you'll want to store files like these in File.applicationStorageDirectory.
So to create the file do:
File.applicationStorageDirectory.resolvePath("my-config.xml");
Alternatively, you can let the user choose where to store the file by using File#browseForSave(), which will display a native 'save' window to choose the location.
Writing the content
Open a FileStream for the File in 'write' mode and write an XML string to the file.
var fs:FileStream = new FileStream();
fs.open(file, FileMode.WRITE);
fs.writeUTF(myXmlContent);
fs.close();

look at http://www.adobe.com/devnet/air/flex/quickstart/articles/xml_prefs.html
http://www.thetechlabs.com/xml/how-to-build-a-contact-manager-in-air-using-xml-part-2/
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/filesystem/File.html
var prefsFile:File = File.applicationStorageDirectory;
prefsFile = prefsFile.resolvePath("preferences.xml");
stream = new FileStream();
stream.open(prefsFile, FileMode.WRITE);
stream.writeUTFBytes(outputStringXML);
Or something like:
var saveStr:String = xmlToSave.toXMLString();
var file:File = new File('app-storage:/data.xml');
var fs:FileStream = new FileStream();
fs.open(file, FileMode.WRITE);
fs.writeUTFBytes(saveStr);
fs.close();

Related

Can the Image path for the AddImage method in MigraDoc be used for the Visual Studio project subfolders?

The following example shows a very simple construction for creating a PDF with MigraDoc. The most parts of it are taken from the examples provided in the MigraDoc - Wiki documentation.
I have commented all the most few steps for the job. The path for the "AddImage" points to the subfolder "Images" which is a subfolder of the folder "Resources" created in the root of the console application of a Visual Studio (VS) project.
The most recent MigraDoc library is added to the VS - project via "NuGet" just yesterday.
The application creates the PDF output file unfortunately without the provided image file, if it is used in another machine. Because the execution file has then no access to the subfolders created in VS project.
Is there any solution to this problem?
public class Program
{
static void Main(string[] args)
{
// Create a MigraDoc document
var document = new Document();
// Add a section the document
var section = document.AddSection();
// .....
/*
------------------------------- NOTE -------------------------------
The path "../../Resources/Images/MigraDoc.png" is valid only for the developer machine!
A copy of the content of the "Debug" or "Release" folder any where else does not show the image in the output PDF.
Because such a copy has no access to the subfolder "/Resources/Images" of the Visual Studio project on the developer machine.
*/
section.Headers.Primary.AddImage("../../Resources/Images/MigraDoc.png");
// Create a renderer for PDF that uses Unicode font encoding.
var pdfRenderer = new PdfDocumentRenderer(true);
// Set the MigraDoc document.
pdfRenderer.Document = document;
// Create the PDF document.
pdfRenderer.RenderDocument();
// Save the PDF document...
var filename = "Invoice.pdf";
// Create the output directory
Directory.CreateDirectory("PDF");
// Create the ouptput file path
var savePath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + "\\PDF\\" + filename;
// Delete the output file if it already exists
if (File.Exists(savePath))
File.Delete(savePath);
// Save the output file
pdfRenderer.Save(savePath);
// Start a the default PDF viewer from the operation system
Process.Start(savePath);
}
}
A path beginning with ..\..\ relies on the current directory. For a portable solution, include the images you need in your deployment.
Recommended usage: The MigraDoc Document class has an ImagePath property. If your installation folder has an Images folder, just locate your .EXE file and set the ImagePath property accordingly.

Get resource from another folder in Java

My project has a folder for resource and a folder for src. All my code are in src and all my property files are in resource (it's set as resource root too)
So I want to read xyz.properties in my code as input stream
I tried
InputStream in = getClass().getResourceAsStream("/resources/xyzz.properties");
and
InputStream in = getClass().getResourceAsStream("xyzz.properties");
and neither worked. They all end up being null - only when I move xyzz.properties to the package that has the class that uses the above code does the second code snippet work. What should I be using for the path if I want to keep the current location of the property file?
Like this
InputStream inputStream = getClass().getClassLoader().getResourceAsStream("xyzz.properties");
Or like this
ClassLoader classloader = Thread.currentThread().getContextClassLoader();
InputStream is = classloader.getResourceAsStream("xyzz.properties");

Stream pdfs from url and add it to Zip

I have a mvc 4.5 application where I show a grid. The first column of the grid is a document name. The document name is an hyper link to the actual document that is hosted on our site and is available via a url. The documents can be pdf or doc or ppt. I can access these documents only via url and I do not have access to the actual physical document on our server.
I am providing users an option to select one or many of these documents from the grid and then they can download them. What I am trying to achieve is read each of the selected documents via the url and write it to a zip file and make the zip file downloadable. So users will be downloading one file instead of multiple files.
I have tried to stream the documents via url in memory and then add it to the zip file using ZipArchive Library from Microsoft. This is not working for me.
I was able to add documents that was on disk to zip file using Zip Archive and it works great. But I do not have access to the physical document as I can access the documents only through URL. My next option is to download each of these documents into a temp location on server and then add it to zip file using Zip Archive.But I am trying to avoid downloading files into a temp location
Please suggest how I can achieve reading documents via url in memory and adding each of these document to zip file and make zip file downloadable.
Any help will be appreciated.
Thank you Cbroe for commenting. I figured the answer. The problem was I was reading the pdf from the url and convert it to a memory stream and then was trying to add the memory stream to ZipArchive which was not working but instead I extracted the byte array out of the memory stream and then added it to the zip archive and it worked.
Here is the code snippet that might be useful for some one. My first contribution to Stack OverFlow.
public FileResult DownloadZip()
{
MemoryStream memoryStream = new MemoryStream();
using (var archive = new ZipArchive(memoryStream, ZipArchiveMode.Create, true))
{
var demoFile = archive.CreateEntry("Pdf123.pdf");
var convertedStream = ConvertTobyte("http://www.example.com/Pdf123.pdf");
using (var entryStream = demoFile.Open())
{
entryStream.Write(convertedStream, 0, convertedStream.Length);
}
demoFile = archive.CreateEntry("Pdf456.pdf");
convertedStream = ConvertTobyte("http://www.example.com/Pdf456.pdf");
using (var entryStream = demoFile.Open())
{
entryStream.Write(convertedStream, 0, convertedStream.Length);
}
}
//This option is to write the zip to your local disk
using (var fileStream = new FileStream(#"C:\Temp\test.zip", FileMode.Create))
{
memoryStream.Seek(0, SeekOrigin.Begin);
memoryStream.CopyTo(fileStream);
}
//This option is to donload the zip via browser
memoryStream.Seek(0, SeekOrigin.Begin);
return new FileStreamResult(memoryStream, "application/zip")
{
FileDownloadName = "Archive.zip"
};
}
private static byte[] ConvertTobyte(string fileUrl)
{
byte[] imageData = null;
using (var wc = new System.Net.WebClient())
imageData = wc.DownloadData(fileUrl);
return imageData;
}

Upload file with meta data and checkin to sharpoint folder using Client Object Model

Hi I'm trying to upload a file to sharepoint 2010 using the client api with meta data and also checkin the file after I'm done. Below is my code:
public void UploadDocument(SharePointFolder folder, String filename, Boolean overwrite)
{
var fileInfo = new FileInfo(filename);
var targetLocation = String.Format("{0}{1}{2}", folder.ServerRelativeUrl,
Path.AltDirectorySeparatorChar, fileInfo.Name);
using (var fs = new FileStream(filename, FileMode.Open))
{
SPFile.SaveBinaryDirect(mClientContext, targetLocation, fs, overwrite);
}
// doesn't work
SPFile newFile = mRootWeb.GetFileByServerRelativeUrl(targetLocation);
mClientContext.Load(newFile);
mClientContext.ExecuteQuery();
//check out to make sure not to create multiple versions
newFile.CheckOut();
// use OverwriteCheckIn type to make sure not to create multiple versions
newFile.CheckIn("test", CheckinType.OverwriteCheckIn);
mClientContext.Load(newFile);
mClientContext.ExecuteQuery();
//SPFile uploadFile = mRootWeb.GetFileByServerRelativeUrl(targetLocation);
//uploadFile.CheckOut();
//uploadFile.CheckIn("SOME VERSION COMMENT I'D LIKE TO ADD", CheckinType.OverwriteCheckIn);
//mClientContext.ExecuteQuery();
}
I'm able to upload the file but I can't add any meta data and file is checked out. I want to add some meta data and checkin the file after I'm done.
My SharePointFolder class has the serverRelativeUrl of the folder path to upload to. Any help greatly appreciated.
You need a credential before the executeQuery(); and SaveBinaryDirect();
For example:
mClientContext.Credentials = new NetworkCredential("LoginID","LoginPW", "LoginDomain");
SPFile newFile = mRootWeb.GetFileByServerRelativeUrl(targetLocation);
mClientContext.Load(newFile);
mClientContext.ExecuteQuery();

windows 8 modern ui apps - access to data

Where can i find folder with installed modern ui apps? Im developing some app which uses .txt files to store information (win8 doesnot support datebase on arm - facepalm) but they seem to not work properly - thats why i want to access them.
Thanks!
That is not the correct way of doing things in Metro. I assume you mean db files, or txt files. Simply access the local text file from the project folder.
Here is a great tutorial on how you would go about doing so: http://www.codeproject.com/Articles/432876/Windows-8-The-Right-Way-to-Read-Write-Files-in-Win
An example:
private async void ProjectFile()
{
// settings
var _Path = #"Metro.Helpers.Tests\MyFolder\MyFolder.txt";
var _Folder = Windows.ApplicationModel.Package.Current.InstalledLocation;
// acquire file
var _File = await _Folder.GetFileAsync(_Path);
Assert.IsNotNull(_File, "Acquire file");
// read content
var _ReadThis = await Windows.Storage.FileIO.ReadTextAsync(_File);
Assert.AreEqual("Hello world!", _ReadThis, "Contents correct");
}