How to create a downloadable file button on form - download

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');

Related

Download TXT file from URL and save to PC on Flutter Web

I have a problem with my project, I wanna to download a .txt file and save to any directory with Flutter WEB, I think I put the setup but It does not work. Now de .txt file display the content in the same tab of browser, but I want to download this file, here is my code:
void downloadFile(String url) {
html.AnchorElement anchorElement = new html.AnchorElement(href: url);
anchorElement.download = "plantilla_simulador.txt";
anchorElement.dispatchEvent(html.Event.eventType('MouseEvent', 'click'));
anchorElement.style.display = 'none';
anchorElement.click();
}
Perhaps you are trying to download file from another site.
Keep in mind that download attribute only works for same-origin URLs, or the blob: and data: schemes.
This code should download file:
html.AnchorElement(href: 'index.html')
..download = 'some_name.txt'
..style.display = 'none'
..click();
but this one should open file in browser:
html.AnchorElement(href: 'https://raw.githubusercontent.com/flutter/flutter/master/flutter_console.bat')
..download = 'some_name.txt'
..style.display = 'none'
..click();

How to send kendo ui grid data in email as attachments?

I have to send an email, with kendo ui grid data as attachment. If I use excel export the I am not able to auto save the file in project folder at particular location. I am not able to customize the saveAsExcel() method.
So I don't want to save that file in the local folder. Is there any way to do this?
I am not able to auto download the file to specific folder in project. Every time it is asking to save at particular location.
I am trying to auto save the file to specific folder in project and try to attach that file in email.
I am not getting any data in excel while saving grid. Also I want to avoid the save as popup for saving the file.
var grid = $("#MyReport").data("kendoGrid");
var trs = $("#MyReport").find('tr');
var rows = [];
for (var i = 0; i < trs.length; i++) {
var dataItem = grid.dataItem(trs[i]);
rows.push({
cells: [
dataItem
]
})
}
var workbook = new kendo.ooxml.Workbook({
sheets: [
{ title: "EmployeeInfo",
rows: rows
}
]
});
kendo.saveAs({ dataURI: workbook.toDataURL(), fileName: "EmployeeInfo.xlsx" });
}
You are on wrong track. In real case scenario, your project should be on server. When a user clicks export on your grid it will be prompted to save on his local computer not in your project on server. Kendo export is client-side export. You need to export on the server-side, save the file on the server and attache it when sending mail through your project.
Steps to take:
On export button click send params page_size and page
On server-side with that params get data from a database
On server-side with data from a database create a file (excel)
On server-side via mail client attache created file and send it

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');
}
},

Sending data from listener (of httpRequestObserver) to addon panel (iframe)

I am trying to create a fire-bug like extension for firefox which is actually dev-tool extension. I have registered httpRequestObserver to observe http-on-examine-response event. I have a listener with below method implemented.
onDataAvailable: function(request, context, inputStream, offset, count) {
//I get the request URL using request.name
var responseData = getResponseData(); // gets data from inputStream
// Now I need to render this responseData into panel's iframe
}
I have created the above script as a module and included it in main.js. I could not find out how the data from this script could be sent to the script included in panel's HTML.
I read about Content Script and port.emit & port.on but I think Content Script won't come into picture since I don't want to touch the actual page's DOM. Want I want to do is intercept the HTTP response and log it into devtool panel.
Thanks in advance.
Take the response string received and make a data url out of it. Then with your content script do a document.write(dataurl) or do window.location = dataurl.
How to make dataurl:
var responseData = getResponseData();
var dataurl = 'data:text/html,' + encodeURIComponent(responseData);
I hear that widgets in sdk have a content option where you can specify this data url:
'content' option in panel, which would enable you to specify HTML content directly, as you can with a widget, as a result of which I've raised bug 692675.
Can also be done like this:
var HTML = '<html><p>Hi there</p></html>';
var panel = require('panel').Panel({
contentURL: "data:text/html, " + encodeURIComponent(HTML)
});
panel.show();

Download links with TideSDK

I have seen several posts coming close to solving my issue, but I am still not able to accomplish my simple task, which is this:
Imagine I have a window which contains a link to a remote file (most often it will be a zip file). How can I structure and call a function that accesses the file and opens a "Save As" dialogue so that the user can choose where to save the downloaded file? It would be nice to be able to pass different variables from other links to the same function to accomplish the same thing for other downloadable files.
And yes, I am completely new to TideSDK and not exactly a javascript expert, if this is causing much painful slapping of foreheads.
Try this.
Any link with a class of "save-as" will trigger the "Save as" dialog. The file will be saved AFTER the user selects the location, gives it a name and clicks Save. This does use jquery.
Download WordPress
<script>
$(function(){
var currentLink;
$('.save-as').click(function() {
var link = $(this).attr('href');
var filename = link.substring(link.lastIndexOf('/')+1);
currentLink = link;
Ti.UI.currentWindow.openSaveAsDialog(saveComplete, {
title: 'Save As...',
multiple: false,
defaultName : filename
});
return false;
}); // End save as.
var saveComplete = function(results) {
if(results.length>0) {
var downloadFile = results[0];
console.log("Download the file");
var httpClient = Ti.Network.createHTTPClient();
httpClient.open('GET', currentLink);
httpClient.receive(function(data) {
var file = Ti.Filesystem.getFile(downloadFile);
var fileStream = file.open(Ti.Filesystem.MODE_APPEND);
fileStream.write(data);
fileStream.close();
});
}
};
});
</script>

Resources