Export to excel with images in JavaScript - javascript

I have the following code :
<button id="myButtonControlID" onclick="ExcelExport();">Export to Excel</button>
<script>
function ExcelExport(){
var imgurl = "https://image.flaticon.com/icons/svg/60/60752.svg"
var HTMLtext="<table><tr><td>Image export</td><td><img src='"+imgurl+"'style='width:500px;height:600px;'></img></td></tr></table>";
window.location.href = 'data:application/vnd.ms-excel,' + encodeURIComponent(HTMLtext);
sa = window.location.href;
return (sa);
}
</script>
With this I am able to export an excel file as XLS but since the image is a link,it is only displayed when I am connected to the internet. Is there a way to embed the image to excel in such a way that the image is not a link and it is displayed even when you are offline?

Related

How to save pdf file from Firebase in local folder from web

How do I download and save a pdf file from Firebase storage.
During upload I save upload infomation on real time firebase node like this:
while my storage is as follow:
My save btn is linked to this function:
function savepdfFile(){
let selectedGroup = document.getElementById("groupname");
let selectedDate = document.getElementById("dateId");
let selectedGroupValue = selectedGroup.value;
let selectedDateValue = selectedDate.value;
let mdate = "";
mdate = changeDateForamat(selectedDateValue);
downloadPDFDoc(selectedGroupValue,mdate);
}
I retrieve information of this pdf file from firebase real time node as follow when a save button is clicked;
function downloadPDFDoc(groupname,mdate){
let pdfName, pdfUrl;
firebase.database().ref('SessionPDFStorage').once('value',function(snapshot){
snapshot.forEach(
function(ChildSnapshot){
//let mLocation = ChildSnapshot.val().province;
let mGroup = ChildSnapshot.val().selectedGroup;
let mydate = ChildSnapshot.val().currentDate;
let ddate = changeDateForamat(mydate);
if(groupname == mGroup && mdate == ddate){
pdfName = ChildSnapshot.val().fileName;
pdfUrl = ChildSnapshot.val().fileImageUri;
}
}
);
loadScannedImage(pdfName,pdfUrl);
});
}
How do I develop the loadScannedImage(pdfName,pdfUrl) funtion below to using the above information to save the file on the local folder dynamically? example automatically save this pdf to a local c:\download directory.
function loadScannedImage(pdfName,pdfUrl){
}
I have seen some implementation of FileSaver.js and Blob.js but it is not quite clear how I would use them.
Any help will be appreciated.
I created an anchor element and pass both pdf name and its url link as as attributes follow inside loadscnnedImage:
function loadScannedImage(pdfName,ImgUrl){
var element = document.createElement('a');
element.setAttribute('href',`${ImgUrl}`);
element.setAttribute('download', pdfName);
document.body.appendChild(element);
element.click()
}
but I would have liked to dynamically safe the pdf file directly to browsers default downloaded folder on the machine. But this works too

how to save an mp3 file on live website in ASP.net?

I get the complete video data from youtube link which I then convert to MP3 format. All of this works on localhost but is not working on the when I put it on live.
View:
<form method="post" style="display:inline" action="#Url.Action("DownloadVideoFunc","Home",new {#link=item.Link})">
#Html.Hidden("returnURL", this.Request.RawUrl)
<button onclick="CallToastFunction();" type="submit" class="btn btn-outline-primary">
Download MP3 too
</button>
</form>
This is the controller function:
var youTube = YouTube.Default; // starting point for YouTube actions
var video = youTube.GetVideo(link); // gets a Video object with info about the video
var path= Path.GetTempPath();
System.IO.File.WriteAllBytes(path + video.FullName, video.GetBytes());//#"D:\"
var inputFile = new MediaFile { Filename = path + video.FullName };
var outputFile = new MediaFile { Filename = $"{path + video.Title}.mp3" };
using (var engine = new Engine())
{
engine.GetMetadata(inputFile);
engine.Convert(inputFile, outputFile);
}
System.IO.File.Delete(path + video.FullName);
It doesn't save the files and gives error. I can work with any JS function or C# function for getting the file to download.

Export AngularJS table to .CSV directing to about:blank page?

I currently have a Javascript function which exports my AngularJS table of JSON data to a .csv file by div ID as shown:
$scope.exportToExcel = function (tableId) { // ex: '#my-table'
var exportHref = Excel.tableToExcel(tableId, 'WireWorkbenchDataExport');
$timeout(function () { location.href = exportHref; }, 100); // trigger download
}
Which I call like so
<button style="float: right;" class="btn btn-link" ng-click="exportToExcel('#codeProjectsTable')" filename="test.csv">
<span class="glyphicon glyphicon-share"></span>
Export table
</button>
This works well for data sets with ~400 records or less (20 columns per record), but I'm trying to export nearly 1,000 records for further reporting and I am simply being directed to an about:blank page.
I'm suspecting that it is either
Unable to export this large of a file
Timing out because of a long request
Thanks in advance.
I used following logic for to export
var fileName = 'users_export_' + moment().format('YYYY-MM-DD') + '.csv';
var url = URL.createObjectURL(new Blob([response.data]));
var a = document.createElement('a');
a.href = url;
a.download = fileName;
a.target = '_blank';
a.click();
And its working for large for large file/data

Print static SWF on HTML

I've been trying really hard on this with no results, hope someone could help me out.
What I have is an HTML page with a ActionScript2 SWF object embedded. The SWF is a static sprite that gets dynamic text from the javascript. So basically looks like an image.
I want to print out the whole page HTML + SWF included, but as expected on the possition of the SWF object appears a blank square.
I print the page using javascript window.print();
Is there anyway to take a snapshot of the SWF or render it like an image? to show it on the print preview and on the printed document?
The size of the flash object is 600x381px;
Thank you!
Sorry guys、but I finally came up with a solution.
Thanks for having a look anyways.
What I first did is to convert the flash file from ActionScript2 to ActionScript3 to be able to use the as3corelib of Mike Chambers https://github.com/mikechambers/as3corelib.
From this library I used the JGPEncoder as seen below
import flash.external.ExternalInterface;
import flash.display.JPEGEncoderOptions;
import com.adobe.images.JPGEncoder;
import flash.display.BitmapData;
import flash.utils.ByteArray;
import flash.display.Bitmap;
import flash.net.URLRequest;
import flash.net.URLLoader;
import flash.events.Event;
function snapShot(){
var imgBM:Bitmap;
var myBitmapData:BitmapData = new BitmapData(stage.stageWidth, stage.stageHeight);
myBitmapData.draw(stage);
imgBM=new Bitmap(myBitmapData);
addChild(imgBM);
var imgBA:ByteArray;
var jpgEncoder:JPGEncoder = new JPGEncoder(90);
//-------Send image to php
imgBA = jpgEncoder.encode(myBitmapData);
var sendHeader:URLRequestHeader = new URLRequestHeader('Content-type', 'application/octet-stream');
var sendReq:URLRequest = new URLRequest("parseimg.php");
sendReq.requestHeaders.push(sendHeader);
sendReq.method = URLRequestMethod.POST;
sendReq.data = imgBA;
var sendLoader:URLLoader = new URLLoader;
sendLoader.addEventListener(Event.COMPLETE, imageSentHandler);
sendLoader.load(sendReq);
}
var imagePath:String;
function imageSentHandler(event:Event):void {
var dataStr:String = event.currentTarget.data.toString();
var resultVars:URLVariables = new URLVariables();
resultVars.decode(dataStr);
imagePath = "http://" + resultVars.base + resultVars.filename;
}
The next step was to create the php that receives the ByteArray and creates the image file on the server.
<?php
if ( isset ( $GLOBALS["HTTP_RAW_POST_DATA"] )) {
$filename = "your_image_name.jpg";
$fp = fopen( $filename,"wb");
fwrite( $fp, $GLOBALS[ 'HTTP_RAW_POST_DATA' ] );
fclose( $fp );
echo "filename=".$filename."&base=".$_SERVER["HTTP_HOST"].dirname($_SERVER["PHP_SELF"]); } ?>
Finally and with the image already in a folder on the server, I just had to hide the Embedded flash object using CSS, and show up the image instead.
I got this using jquery on document.ready
var d = new Date();
setTimeout(function(){
$("#imgflash").attr("src", "your_image_name.jpg?"+d.getTime());
setTimeout("window.print();", 200);
},1000);
As I'm pretty new on ActionScript and Flash, any improvements on this code are kindly accepted.

Downloading Javascript File from Website using Python

I am trying to use python to download the results from the following website:
http://david.abcc.ncifcrf.gov/api.jsp?type=GENBANK_ACCESSION&ids=CP000010,CP000125,CP000124,CP000124,CP000124,CP000124&tool=chartReport&annot=KEGG_PATHWAY
I was attempting to use mechanize before I realized that the Download File is written in javascript which mechanize does not support. My code so far opens the web page as shown below. I am stuck on how to access the Download link on the web page in order to save the data onto my machine.
import urllib2
def downloadFile():
url = 'http://david.abcc.ncifcrf.gov/api.jsp?type=GENBANK_ACCESSION&ids=CP000010,CP000125,CP000124,CP000124,CP000124,CP000124&tool=chartReport&annot=KEGG_PATHWAY'
t = urllib2.urlopen(url)
s = t.read()
print s
The results that are printed are
<html>
<head></head>
<body>
<form name="apiForm" method="POST">
<input type="hidden" name="rowids">
<input type="hidden" name="annot">
<script type="text/javascript">
document.apiForm.rowids.value="4791928,3403495,...."; //There are really about 500 values
document.apiForm.annot.value="48";
document.apiForm.action = "chartReport.jsp";
document.apiForm.submit();
</script>
</form>
</body>
</html>
Does anybody know how I can select and move to the Download File page and save that file to my computer?
After some more research on that link, I came up with this. You can definitely use mechanize to do it.
import mechanize
def getJSVariableValue(content, variable):
value_start_index = content.find(variable)
value_start_index = content.find('"', value_start_index) + 1
value_end_index = content.find('"', value_start_index)
value = content[value_start_index:value_end_index]
return value
def getChartReport(url):
br = mechanize.Browser()
resp = br.open(url)
content = resp.read()
br.select_form(name = 'apiForm')
br.form.set_all_readonly(False)
br.form['rowids'] = getJSVariableValue(content, 'document.apiForm.rowids.value')
br.form['annot'] = getJSVariableValue(content, 'document.apiForm.annot.value')
br.form.action = 'http://david.abcc.ncifcrf.gov/' + getJSVariableValue(content, 'document.apiForm.action')
print br.form['rowids']
print br.form['annot']
br.submit()
resp = br.follow_link(text_regex=r'Download File')
content = resp.read()
f = open('output.txt', 'w')
f.write(content)
url = 'http://david.abcc.ncifcrf.gov/api.jsp?type=GENBANK_ACCESSION&ids=CP000010,CP000125,CP000124,CP000124,CP000124,CP000124&tool=chartReport&annot=KEGG_PATHWAY'
chart_output = getChartReport(url)

Categories