Open a file in flex 3 - apache-flex

How can I create a .txt file using flex 3? And I want to write some datas in to this file...... Is it possible? Any one can help me?
Thanks in advance..
Nimmy

//set File object
var file:File=File.documentsDirectory;
file=file.resolve(“myFile.txt”);
//set Stream object
var stream:FileStream=new FileStream();
//set FileMode
stream.open(file, FileMode.READ);
var data:String= stream.readUTFBytes(Stream.bytesAvailable);
//close file
stream.close();
different FILEMODE:
FileMode.APPEND: write only, append
new data to the bottom of the file;
FileMode.READ: read only, file must exist;
FileMode.UPDATE: both read / write the
data where positioned where you want;
FileMode.WRITE: write only , if file
doesn't not exist, will be created
otherwise will be overwritten;

Related

Dynamics AX CompanyImage to Image File

I need to export the images stored in CompanyImage table to a image files.
How can I do it?
Reading the table I obtain a Bitmap field but how to know the type of image to build the correct extension and save it to file?
Finally I found the solution.
To export the image from CompanyImage:
// Grant clrinterop permission.
new InteropPermission(InteropKind::ClrInterop).assert();
image = new Image();
image.setData(companyImage.Image);
result = image.saveImage(#"c:\test.jpg",3);
CodeAccessPermission::revertAssert();
To know the original type:
image.saveType();
The above code wouldn't work right away. Here's the code that would run:
bindata bin = new bindata();
str content;
container image;
CompanyImage companyImage;
;
select companyImage;
image = companyImage.Image;
bin.setData(image);
content=bin.base64Encode();
AifUtil::saveBase64ToFile("c:\\temp\\test.tif", content);

PDFizer: how to inset picture in generated pdf document?

i'm using PDFizer library for .NET from here - PDFizer
and i need help... how i can convert all html document(including pictures stored in it) to PDF with this library? Now i can only generate pdf without images...
After some testing, this is what you need to do:
Create a Folder in which you will have all of your Images.
If you already have an instance of Pdfizer.HtmlToPdfConverter change the ImagePath Attribute to point to the folder where your images reside.
Include the <img> tags in your html code.
Make sure the images are in the folder.
Note: I tried adding Png files and got a conversion error. Here is an example I took from the site you provided, plus my modifications:
System.Text.StringBuilder sbHtml = new System.Text.StringBuilder();
sbHtml.Append("<html>");
sbHtml.Append("<body>");
sbHtml.Append("<font size='14'>My Document Title Line</font>");
sbHtml.Append("<img src='trollface.jpg' />");
sbHtml.Append("<br />");
sbHtml.Append("This is my document text");
sbHtml.Append("</body>");
sbHtml.Append("</html>");
//create file stream to PDF file to write to
using (System.IO.Stream stream = new System.IO.FileStream
(sPathToWritePdfTo, System.IO.FileMode.OpenOrCreate))
{
// create new instance of Pdfizer
Pdfizer.HtmlToPdfConverter htmlToPdf = new Pdfizer.HtmlToPdfConverter();
// open stream to write Pdf to to
htmlToPdf.Open(stream);
htmlToPdf.ImagePath = Server.MapPath(ResolveUrl("~/Images"));
// write the HTML to the component
htmlToPdf.Run(sbHtml.ToString());
// close the write operation and complete the PDF file
htmlToPdf.Close();
}
}
Good luck!

Using fileupload in listview edittemplate asp.net

In a ListView edittemplate, I need to allow the user to replace an image. When the form is submitted for updating how can I determine if the user is uploading a new image and get that file info?
Thanks,
James
You could try something like this if you want to compare file size. Granted file size comparison is not the best but FileInfo has lots of other attributes you should be able to use to make sure.
FileInfo oldFileInfo; // get old file's fileInfo
var tempPath = "some-temp-path-";
var tempFile = String.Format("{0}\{1}", tempPath, FileUpload1.FileName);
FileUpload1.SaveAs(tempFile);
FileInfo tempFileInfo = new FileInfo(tempFile);
if(tempFileInfo.Length == oldFileInfo.Length)
{
// ask to upload a different image
}
else
{
// do other stuff
}
Grab the name off of the FileUpload.PostedFile.

File reading and writing inside asp.net web application

I have developed an ASP.NET web application in Visual Studio 2008.
I have an HTML document and one text file in the application, but both are not inside my application.
Both are outside, so when I try to run the same application in another system, I get an error because I am missing those files.
I want to include the files inside the application when I deploy it.
Below is the code I use to read and write the files:
//file write test.txt
FileStream file1 = new FileStream("test.txt", FileMode.Create, FileAccess.Write);
// Create a new stream to write to the file
StreamWriter sw1 = new StreamWriter(file1);
// Write a string to the file
sw1.Write(emailId);
// Close StreamWriter
sw1.Close();
// Close file
file1.Close();
// *** Write to file ***
// Specify file, instructions, and privelegdes
FileStream file = new FileStream("test.html", FileMode.Create, FileAccess.Write);
// Create a new stream to write to the file
StreamWriter sw = new StreamWriter(file);
// Write a string to the file
sw.Write(BodyLiteral.Text);
// Close StreamWriter
sw.Close();
// Close file
file.Close();
// *** Read from file ***
// Specify file, instructions, and privelegdes
file = new FileStream("test.txt", FileMode.OpenOrCreate, FileAccess.Read);
// Create a new stream to read from a file
StreamReader sr = new StreamReader(file);
// Read contents of file into a string
string cval = sr.ReadToEnd();
Response.Write(cval);
// Close StreamReader
sr.Close();
// Close file
file.Close();
//html file reading
string text = File.ReadAllText(#"D:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\test.html");
Both of my files are in: D:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\
How can I deploy these two files with application so that the program will work on another system?
Your best bet would be Server.MapPath().
Example:
Put the files inside folder "file" (you can make a folder in your solution, by right clicking your solution and choose add folder)..
then right click the folder..choose existing item , and then choose your files..
To make the path to your files local.. use the follow
Server.MapPath("~\\files\\test.html");
Your code modified
FileStream file = new FileStream( Server.MapPath("~\\files\\test.html"), FileMode.Create, FileAccess.Write);
Best thing to do is to add the files to your solution.
In solution exploere you can right-click and add an existing item. Change your code to read from that location, so when you deploy your code it will be in a known location and part of the deployment.

How can you save out a String into a file in AS3?

I have a string the user has typed and I want to save it into a file on the users harddrive. Can you do that? And if so, how?
Yes you can, with FileReference.
This is basically how it's done:
var bytes:ByteArray = new ByteArray();
var fileRef:FileReference=new FileReference();
fileRef.save("fileContent", "fileName");
Doesn't look too hard, does it?
And here's a video-tutorial on it too:
http://www.gotoandlearn.com/play?id=76
And the documentation:
http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/
Hope that helps.
Since I had a function to output bytes to a file (because I was doing something with bitmaps), I reused it to output a string as well, like this:
var filename:String = "/Users/me/path/to/file.txt";
var byteArray:ByteArray = new ByteArray();
byteArray.writeUTFBytes(someString);
outFile(filename, byteArray);
private static function outFile(fileName:String, data:ByteArray):void {
var outFile:File = File.desktopDirectory; // dest folder is desktop
outFile = outFile.resolvePath(fileName); // name of file to write
var outStream:FileStream = new FileStream();
// open output file stream in WRITE mode
outStream.open(outFile, FileMode.WRITE);
// write out the file
outStream.writeBytes(data, 0, data.length);
// close it
outStream.close();
}
In addition, you must have Flash Player 10 and a Flex Gumbo SDK installed in your Flex Builder 3.
You can also have a look the following example:
http://blog.flexexamples.com/2008/08/25/saving-files-locally-using-the-filereference-classs-save-method-in-flash-player-10/
In Flex 3 no you can't do it unless you upload the file to the server and then download the file via a url to the desktop.
In Air or Flex 4 you can save it directly from the application to the desktop as detailed above.

Resources