I need to download a file for example say, a pdf file from a url and store it in applicationsDirectory or ApplicationStorage directory , I have a code to download but its opening save dialog box to get the userinput for where to save the downloaded file.
This is the code that I am using
downloadURL.url = urlLocation;
configureListeners(file);
file.download(downloadURL);
I need to download the file with out opening any window, and file needs to be downloaded to ApplicationStorage directory.
Thanks in advance
You need the File and FileStream Classes... first load what you need as a binary URLLoader, retrieve the ByteArray, and then save it to the location you want with something like this:
var file:File = new File(File.applicationStorageDirectory.nativePath+"/myfile.jpg");
var fileStream:FileStream = new FileStream();
fileStream.open(file, FileMode.WRITE);
fileStream.writeBytes(bytes);
fileStream.close();
Where "bytes" is the bytearray you loaded...
var loader:URLLoader = new URLLoader ();
var request:URLRequest = new URLRequest ();
request.url = "http://" + "your file adress here";
loader.load(request);
loader.addEventListener(Event.COMPLETE , function (e:Event):void {
var file:File = new File(File.applicationStorageDirectory.nativePath+"/yourfile name here");
var fileStream:FileStream = new FileStream();
fileStream.open(file, FileMode.WRITE);
fileStream.writeBytes(loader.data);
fileStream.close();
});
Related
Here is what I'm trying to accomplish.
I am creating an asp.net MVC application. My restrictions are that I cannot programmatically save anything to the file structure of the server, so I can't save it as a physical file on the host, and then grab it for client download.
I am loading a PDF to a stream, extracting information from the PDF, dynamically building an excel file, and then offering the file for download to the client. My code is below.
// Loads the incoming PDF document to stream
PdfDocument doc = new PdfDocument();
using (var stream = model.BudgetPdfFile.OpenReadStream())
{
doc.LoadFromStream(stream);
}
var pageCount = doc.Pages.Count;
var date = DateTime.Now.ToShortDateString().Replace("/", "-");
// Extracts data from the PDF and separates it by NewLine
SimpleTextExtractionStrategy strategy = new SimpleTextExtractionStrategy();
StringBuilder allText = new StringBuilder();
for (var i = 0; i < pageCount; i++)
{
allText.Append(doc.Pages[i].ExtractText(strategy));
}
var fullDocText = allText.ToString();
List<string> linesList = new List<string>(fullDocText.Split(new[] { Environment.NewLine }, StringSplitOptions.None).ToList());
// generates a comparison list for output data manipulation from static data
var finalList = linesList.BuildFinalList(budgetItems);
// creates a new Spire.PDF.Workbook for the final output excel file
var result = new Workbook();
// checks for whether the submitted budget is for a case in litigation or not and builds the correct excel workbook
if (model.isTrial)
{
result = ExportExcelBudget.TrialBudgetSheet(model, finalList);
}
else
{
result = ExportExcelBudget.PreTrialBudgetSheet(model, finalList);
}
Absolutely everything up to the last section below works perfectly. However, I cannot figure out how to load the workbook into a new stream and then return the file for download.
// saves the final workbook to a stream and offers it for download to the client
Stream outStream = new MemoryStream();
var fileName = "Budget Report_" + model.ClaimNumber + "_" + date + ".xlsx";
var contentType = "application/vnd.ms-excel";
result.SaveToStream(outStream, Spire.Xls.FileFormat.Version2016);
return File(outStream, contentType, fileName);
I've searched and tried multiple different variations but when the application hits the return File(), it returns a null.
I've stepped through execution and the results seem to be there, but it's not passing anything. Any help on what is wrong here would be greatly appreciated.
Stream outStream = new MemoryStream();
var fileName = "Budget Report_" + model.ClaimNumber + "_" + date + ".xlsx";
var contentType = "application/vnd.ms-excel";
result.SaveToStream(outStream, Spire.Xls.FileFormat.Version2016);
**outStream.Position = 0;**
return File(outStream, contentType, fileName);
Had to reset the stream position to 0. Working perfectly now.
I have an asp.net reusable control when directly clicking on file is downloaded with file extension .ashx, that happening only on IE, and its working fine on firefox.
This is how im calling the Control:
<a data-fancybox-type="iframe" class="fancybox" data-fancybox-group="gallery" title="fileName" href="AttachmentControl/DownloadAttachment.ashx?SourceId=6&id=1&attachmentSource=download"><i class="fa fa-file-image-o"></i> <span>fileName</span></a>
This Control is calling a WCF Service as bellow:
AttachmentSvc.IAttachmentService clientDownload = new AttachmentSvc.AttachmentServiceClient();
AttachmentSvc.DownloadRequest requestData = new AttachmentSvc.DownloadRequest();
AttachmentSvc.RemoteFileInfo fileInfo = new AttachmentSvc.RemoteFileInfo();
requestData.FileName = fileName;
if (!String.IsNullOrEmpty(requestData.FileName))
{
fileInfo = clientDownload.DownloadFile(requestData);
byte[] arrayFile = ReadFully(fileInfo.FileByteStream);
file = Convert.ToBase64String(arrayFile);
}
And the code for the wcf service itself is the following:
RemoteFileInfo result = new RemoteFileInfo();
var templateDestinationFolderPath = PSSConfigHelper.AttachmentsUrlPrimaryNode();
string filePath = Path.Combine(#templateDestinationFolderPath, request.FileName);
System.IO.FileInfo fileInfo = new System.IO.FileInfo(filePath);
// check if exists
if (!fileInfo.Exists) throw new System.IO.FileNotFoundException("File not found", request.FileName);
// open stream
System.IO.FileStream stream = new System.IO.FileStream(filePath, System.IO.FileMode.Open, System.IO.FileAccess.Read);
// return result
result.FileName = request.FileName;
result.Length = fileInfo.Length;
result.FileByteStream = stream;
I have now gotten the idea of saving multiple components text into one file. But now when I open that file all the text in the file goes into the text of only one component. Here is the code,
<s:click>
var f:File = File.desktopDirectory;
f.browseForSave("Save As");
f.addEventListener(Event.SELECT, function (event:Event):void {
var stream:FileStream = new FileStream();
stream.open((event.target as File),FileMode.WRITE);
stream.writeUTFBytes(rte.htmlText);
stream.writeUTFBytes(ta.text);
stream.writeUTFBytes(rich.text);
stream.close();
});
</s:click>
</s:Button>
<s:click>
var f:File = File.desktopDirectory;
f.browseForOpen("Select file to open", []);
f.addEventListener(Event.SELECT, function (event:Event):void {
var fs:FileStream = new FileStream();
fs.open(event.target as File, FileMode.READ);
rte.htmlText = fs.readUTFBytes(fs.bytesAvailable);
ta.text = fs.readUTFBytes(fs.bytesAvailable);
fs.close();
});
</s:click>
</s:Button>
Some suggestions?
You already read all the stream from the fs.
As soon as you execute once fs.readUTFBytes(fs.bytesAvailable); the read header/pointer will be indicating to your end of your file, so in the next line the bytesAvailable will be zero.
I suggest you use the following code:
<s:click>
var f:File = File.desktopDirectory;
f.browseForOpen("Select file to open", []);
f.addEventListener(Event.SELECT, function (event:Event):void {
var fs:FileStream = new FileStream();
fs.open(event.target as File, FileMode.READ);
var dataStr:String = fs.readUTFBytes(fs.bytesAvailable);
rte.htmlText = dataStr;
ta.text = dataStr;
// if you want only the non formatted text use: ta.text = rte.text;
fs.close();
});
</s:click>
I need to export charts and data tables to pdf file in flex application.
For this we can user AlivePDF but i need to export to local not server.
Can we export to local system prompting user to select the location to export?
Thanks in advance.
Since FP10 the FileReference Class should support this via the save() function. The code to do this in Flash Player 10 or better is shown below:
var bytes:ByteArray = pdf.save(Method.LOCAL);
var file:FileReference = new FileReference();
file.save(bytes, "myPDF.pdf");
Try this
var pdfFile:PDF = new PDF();
var pdfByteArray:ByteArray = new ByteArray ();
pdfByteArray = pdfFile.save(Method.LOCAL);
With the latest version of AlivePDF (0.1.5 RC), you can do this:
var element:IBitmapDrawable; // Chart to export
var pdf:PDF = new UnicodePDF();
pdf.addPage();
var bitmapData:BitmapData = new BitmapData(element.width, element.height, false, 0xffffff);
try{
bitmapData.draw(element as IBitmapDrawable);
}catch(e:*)
{
throw new Error("bitmap draw failed");
}
var jpegencoder:JPEGEncoder = new JPEGEncoder(100);
var byteArray:ByteArray = jpegencoder.encode(bitmapData);
pdf.addImageStream(byteArray);
var file : FileReference = new FileReference()
file.save(pdf.save(Method.LOCAL),"my.pdf");
i want to uplod csv file in to java from flex screen ? How can i do that ? is it possible,if it is please tell me any one ?
Use the FileReference class:
http://livedocs.adobe.com/flex/3/langref/flash/net/FileReference.html
var loader:URLLoader = new URLLoader();
var req:URLRequest = new URLRequest(serverUrl);
req.data = csvString;
req.method = URLRequestMethod.POST;
loader.load(req);