How can I activate an AJAX script using PowerShell? - ajax

I try to automate the downloading of a PDF file that I can get in clicking manually on PDF icon on a specific html page.
I have decided to automate this task using PowerShell.
I have "already" written first part
$page = Invoke-WebRequest https://journal.cinetelerevue.sudinfo.be
Now I try to activate click event on ReadPDF button so that PDF file is downloaded.
How can I do that ?
If I analyze html file returned by Invoke-WebRequest I can find following javascript code
function readPdf()
{
blackOverlay.fadeIn(),adp_loader.fadeIn();
var e = window.open("","pdf_view");
e.document.body.innerHTML="<p>Veuillez patienter pendant le chargement du pdf. <p><img src='"+path+"design/loader.svg' style='position:absolute;top:50%;left:50%;' />"
,$.ajax({type:"GET",url:urlPdf,success:function()
{
e.location.href=urlPdf,blackOverlay.fadeOut(),adp_loader.fadeOut()
}
,error:function(e)
{
catch404Error(e)}
})
}
How can I activate this function using PowerShell so that I can read PDF file ?
Is what I want possible using PowerShell ?

Related

How to create a downloadable file button on form

I want to create a button on a purchase order form to download in one file all the different item lines in a csv file.
I created a suitlet file and a user event script.
The user event script create a button that redirect on the url of my suitlet script to execute a function.
For now a create a file csv in a folder, but I don't know how redirect on the file url or to directly download the file
Load the file, get the url property (it's not a link to the NS record/file it's a download link), open the url.
In SuiteScript 1.0
var file = nlapiLoadFile('file_id');
var url = "https://system.netsuite.com" + file.getURL();
window.open(url, '_blank');
In SuiteScript 2.0
var filePath = file.load({
id: 'Images/Desert.jpg'
});
var url = file.url;
window.open(url, '_blank');

Downloaded Excel file with Macros is corrupted via MVC ajax

I am trying to download an Excel file from MVC Ajax call, but everytime it downloads, I cannot open the file, because it says it is corrupted.
Following is my Ajax call:
function Download() {
$.ajax({
url: '/Home/ExcelDownload',
type: "POST",
success: function (result) {
if (result !== null || result !== "") {
$('<iframe src=' + result + ' frameborder="0" scrolling="no" id="myFrame"></iframe>')
.appendTo('body');
}
//var iframe = document.getElementById('invisible');
//iframe.src = result;
},
error: function (data) {
}
});}
From my controller I call the action method like this:
string host = Request.Url.Authority;
return Json("http://" + host + "/ExcelTemplates/EInvoiceTemplateNew.xlsm");
I am having macros enabled in Excel as well.
The file downloads properly but when I try to open it, it gives me a warning about being from a trusted source, and on clicking yes, I get another dialog saying "The workbook cannot be opened or repaired By Microsoft excel because it is corrupt".
Any help or suggestions or workarounds to make my code working.
Thanks In Advance!!!..
I'm not able to do a project and try the excel library right now as I'm at work, so I'm just doing this much quickly.
To start, I think you can get away from using ajax completely. Which is always nice because it's less code to maintain, you can use a normal anchor tag:
Download
This won't redirect the page, because the browser should interpret the content-disposition/Mime type, and just download the file.
Then you can try returning the file like this, where you first read the file (in this case into a stream, but you could do it as a byte[] as well as File has a overload for it), and then return it through your controller by using File and FileResult.
E.g:
public FileResult GetExcelDocument(int id)
{
string filePath = GetPathToDocumentWithId(id); //However you get the excel file path
return File(System.IO.File.OpenRead(filePath),
System.Web.MimeMapping.GetMimeMapping(filePath),
"filename.xlsm");
}
This website says that the content type/MIME of .xlsm is "application/vnd.ms-excel.sheet.macroEnabled.12", but I think GetMimeMapping should work.
Optionally, if you want the file to download in a new tab, you can set the target of the anchor tag to "_blank" and it should open a new tab and download the file there.
Let me know what the result is.
If you're set on using JQuery and ajax, I found this example which follows along your previous attempt:
From Here
var $idown; // Keep it outside of the function, so it's initialized once.
downloadURL : function(url) {
if ($idown) {
$idown.attr('src',url);
} else {
$idown = $('<iframe>', { id:'idown', src:url }).hide().appendTo('body');
}
},

Save php generated excel in react

I'm using laravel excel package to generate xls file. When I'm sending get request I can see excel file in my response(random characters), however there is no prompt to save it(I guess react blocking it on front end). What would be the right way to implement it?
React onclick listener:
function handleClick(e) {
e.preventDefault();
$.get('excel', function(data) {
//alert(data);
});
}
Laravel controller:
public function generateExcel()
{
return Excel::create('Filename', function($excel) {
$excel->sheet('Sheetname', function($sheet) {
// Sheet manipulation
});
})->download('xls');
}
Yes, react is blocking your download. The download prompt happens because of the HTTP headers. So, guessing you have a laravel url for you xls like /laravel/get/xls you should have.
Get Xls
In this way you'll open a new tab in which the http headers will prompt for the download.

How can give name for generate pdf using mpdf codeigniter

if anybody konws this,please help me.how to give name to pdf.
i generated pdf using mpdf codeigniter. on click of a button the pdf wil be viewed. but how can give name for that pdf? it shows 1 on the top of the pdf.how can i give name to that pdf?
My controller
public function viewpdf($key,$option) {
if($option=='1')
{
$searchdata['fetchproduct']=$this->b2bproduct_model->fetch_productdata1($key);
}
if($option=='2')
{
$searchdata['fetchproduct']=$this->b2bproduct_model->fetch_productdata2($key);
}
if($option=='3')
{
$searchdata['fetchproduct']=$this->b2bproduct_model->fetch_productdata3($key);
}
$html=$this->load->view('moderator/pdf_data', $searchdata,true);
//this the the PDF filename that user will get to download
$pdfFilePath = "shany.pdf";
//load mPDF library
$this->load->library('m_pdf');
//generate the PDF from the given html
$this->m_pdf->pdf->WriteHTML($html);
//download it.
$this->m_pdf->pdf->Output($pdfFilePath, "I");
}
Use this code.
$mpdf=new mPDF();
$mpdf->SetTitle('My Title');
$mpdf->WriteHTML('<p>Hallo World</p>');
$mpdf->Output('filename.pdf');
Set the title for the document. The title is displayed at the top of the Adobe Reader screen when viewing the PDF file
Use code as bellow
public function mypdf() {
$this->load->library('pdf');
$pdf = $this->pdf->load();
$html=$this->load->view('welcome_message',null,true);
$pdf->WriteHTML($html);
// write the HTML into the PDF
$output = 'your_given_name.pdf'; //You can give a name of your generated pdf file or you can create it auto on timestamp by using $output = time().'.pdf'.
$pdf->Output("$output", 'I');
}
If you send data to view page then replace the variable with null in line 4.
For more details please see the tutorial from here.
If you are interested on DOMPDF please see from here.

PlayFramework: Ajax + Drag n' Drop + File Upload + File object in controller?

Does anyone know of a way to upload a file via Ajax and using drag n' drop from the desktop that supports PlayFramework's ability to convert file uploads to a File object?
I've tried several different methods, and nothing works correctly.
Here's my successful attempt:
Edit routes file and add
POST /upload Application.upload
Our controller is Application, I'll be using it to keep it simple.
Edit your Application controller class
public static void upload(String qqfile) {
if (request.isNew) {
FileOutputStream moveTo = null;
Logger.info("Name of the file %s", qqfile);
// Another way I used to grab the name of the file
String filename = request.headers.get("x-file-name").value();
Logger.info("Absolute on where to send %s", Play.getFile("").getAbsolutePath() + File.separator + "uploads" + File.separator);
try {
InputStream data = request.body;
moveTo = new FileOutputStream(new File(Play.getFile("").getAbsolutePath()) + File.separator + "uploads" + File.separator + filename);
IOUtils.copy(data, moveTo);
} catch (Exception ex) {
// catch file exception
// catch IO Exception later on
renderJSON("{success: false}");
}
}
renderJSON("{success: true}");
}
Edit your Application.html in app/views/Application folder/package
#{extends 'main.html' /}
#{set title:'Multiple Uploads' /}
<div id="file-uploader">
<noscript>
<p>Please enable JavaScript to use file uploader.</p>
<!-- or put a simple form for upload here -->
</noscript>
<script>
function createUploader(){
var uploader = new qq.FileUploader({
element: document.getElementById('file-uploader'),
action: '/upload',
debug: true
});
}
// in your app create uploader as soon as the DOM is ready
// don't wait for the window to load
window.onload = createUploader;
</script>
</div>
Edit your main layout: main.html, located in the app/views folder/package and add this line after jQuery
<script src="#{'/public/javascripts/client/fileuploader.js'}" type="text/javascript"></script>
Final notes
Remember to download the script from AJAX Upload Valums, enjoy!
You can also grab the source here.
I tested it in different browsers it works for me at least. Credits to Riyad in Play! mailing list who hinted me about the request.body
P.S: I'm using the one I posted as a comment before
Edit
The answer with code has been added as directed by T.J. Crowder, I agree :)
The simple upload part (not drag&drop just click on "upload a file") is not working with Ie7 & 8 (don't try others ie)
See getting Java Bad File Descriptor Close Bug while reading multipart/form-data http body
Not really sure this will qualify as an answer since I'm not a hundred percent sure it will work. But it should work :)
If I understand you correctly, you want to drag files from the desktop and drop them in a drop zone somewhere in your browser. This triggers an ajax upload call to a play server.
I've got the second part of that working, using a straight jquery ajax post. The files are received just fine. For the first part, I'd try using the dnd support in html 5 (scroll down to Dragging Files):
http://www.html5rocks.com/tutorials/dnd/basics/

Resources