I want to export extjs grid content to excel file. So what i have done already:
i send to the servlet json content of the grid through Ext.Ajax.request, like
Ext.Ajax.request({
url : 'ExportToExcel',
method:'POST',
jsonData: this.store.proxy.reader.rawData,
scope : this,
success : function(response,options) {
this.onExportSuccess(response, options);
},
//method to call when the request is a failure
failure: function(response, options){
alert("FAILURE: " + response.statusText);
}
});
Then in servlet i get json in servlet do some stuff, e.g. create excel file, convert it to bytes array and try to put into response.
In Ext.Ajax.request in success method when i try to do something like this:
var blob = new Blob([response.responseText], { type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet});
var downloadUrl = window.URL.createObjectURL(blob);
var a = document.createElement("a");
a.href = downloadUrl;
a.download = filename;
document.body.appendChild(a);
a.click();
it downloads excel file, but when i open it error occurs, saying format or extension is invalid.
WHY??
Whether it may depend on the encoding?
What i'm doing wrong?
Ext comes with an excel exporter built in. You may want to try using that to see if you get the desired results
grid.saveDocumentAs({
type: 'excel',
title: 'My Excel Title',
fileName: 'MyExcelFileName.xml'
});
From the toolkit : Ext.grid.plugin.Exporter
Related
I would like to use jsreport browser to send data to the server and download it back as xlsx file.
Using jsreport.download(request) would limit the amount of data cause it is a GET. Can I use jsreport.renderAsync() to download a xlsx file just like with pdf?
jsreport.renderAsync returns in promise ArrayBuffer. You can convert it to blob and then use saveAs to download it to the user computer.
<script async="" src="https://cdn.rawgit.com/eligrey/FileSaver.js/e9d941381475b5df8b7d7691013401e171014e89/FileSaver.min.js"></script>
<script>
jsreport.renderAsync({
template: {
content: '<table><tr><td>foo</td></tr></table>',
engine: 'none',
recipe: 'html-to-xlsx'
}
}).then(function (res) {
var dataView = new DataView(res);
var blob = new Blob([dataView], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' });
saveAs(blob, 'a.xlsx')
})
</script>
https://playground.jsreport.net/studio/workspace/HJ0z0yaY/8
It looks like the email package in Meteor now allows adding attachments similar to how MailComposer does. On my server I have:
Meteor.startup( function() {
process.env.MAIL_URL = "smtp://<my maligun info here>";
});
Meteor.methods({
sendEmail: function (to, from, subject, text, attachment) {
// Let other method calls from the same client start running,
// without waiting for the email sending to complete.
this.unblock();
Email.send({
to: to,
from: from,
subject: subject,
text: text,
attachment: attachment
});
}
});
Inside the app I'll have a helper like:
Template.donateEmail.events({
'click #send-donate-email': function() {
var attachment = {
fileName: "Demographics3.numbers",
filePath: "/Users/Opal/Desktop/Demographics3.numbers"
};
var emailCompose = document.getElementById('compose-donate-email').value;
var emailSubject = document.getElementById('subject-donate-email').value;
Meteor.call('sendEmail',
"some#email.com", //Session.get('keyDonateEmailSendList'),
'some2#email.com',
emailSubject,
emailCompose,
attachment)
}
});
I can get emails to send, but sending but there's no attachments. And the documentation is confusing. Anyone have any more info on this? I'm missing something somewhere.
Problem solved. Two mistakes in my code. In the Email.send method, it needs to read
"attachments: <some name>" not "attachment: <some name>".
The second issue is the making sure one specifies the correct absolute path, which in my case on a Mac would be:
"Volumes/Macintosh\ HD/Users/Opal/Desktop/<filename>"
I have found that Apple .numbers files aren't openable, but they will attach. Other files should be OK.
i have some data i need to upload it to the server with an image using
multipart/form-data
but i getting request time out this is my data
var data={
property_name:p_n_txt.value,
friendly_name:f_txt.value,
property_type:clicked,
size:space_Slider.getValue(),
price:price_Slider.getValue(),
number_of_bedrooms:bedrooms_Slider.getValue(),
number_of_bathrooms:bathrooms_Slider.getValue(),
number_of_parkings:p_space_Slider.getValue(),
location:a_txt.value,
features:13,
payment_method:4,
image1_name:"image0.png",
image1:Ti.Filesystem.getFile(Ti.Filesystem.tempDirectory,Ti.App.Properties.getString("filename")),
};
and the httpclient is
var xhr = Ti.Network.createHTTPClient({
onload: function() {
var myData = JSON.parse(this.responseText);
console.log(this.responseText);
},
// function called when an error occurs, including a timeout
onerror : function(e) {
Ti.API.debug(e.error);
console.log(this.status);
console.log(e.error);
},
timeout : 20000
});
xhr.open('POST','http://url/');
xhr.setRequestHeader("enctype", "multipart/form-data");
xhr.setRequestHeader("Content-Type", "image/png");
// xhr.setRequestHeader("Content-Type", "image/jpeg");
xhr.send(data);
});
here is should form data not a json so there is no need to add json.stringify
but i have 4 cases i tried with them
first :with stringify i got HTTP 415 if i added contentType "image/png"
and HTTP 500 if i didn't added
second without stringify when i add added contentType "image/png" i got request time out
and when i didn't add it i get HTTP 413
any idea on how to accomplish this cause i found many question about it but no one was helpful for me
thanks
Why don't you try with encoding your image as base64encoded and pass it on and at the server end just decode it and you would be go enough to proceed.
Ti.Utils.base64encode(image.toBlob()).toString();
This can be sent to server and no headers would be required to sent.
I'm encountering issues with a dashlet that I'm trying to develop for Alfresco. It's a simple drag and drop file upload dashlet using HTML 5's drag and drop and file APIs. For the drop event listener, I call the following function which is seemingly the cause of all the problems:
function handleFileSelect(evt) {
var files = evt.target.files || evt.dataTransfer.files,
tmpForm, tmpDest, tmpMeta, tmpType, tmpName, tmpData;
dropZone.className = "can-drop";
evt.stopPropagation();
evt.preventDefault();
for (var i=0,f;f=files[i];i++) {
tmpForm = document.createElement('form');
tmpDest = document.createElement('input');
tmpDest.setAttribute('type', 'text');
tmpDest.setAttribute('name', 'destination');
tmpDest.setAttribute('value', destination);
tmpForm.appendChild(tmpDest);
tmpMeta = document.createElement('input');
tmpMeta.setAttribute('type', 'text');
tmpMeta.setAttribute('name', 'mandatoryMetadata');
tmpMeta.setAttribute('value', window.metadataButton.value);
tmpForm.appendChild(tmpMeta);
tmpType = document.createElement('input');
tmpType.setAttribute('type', 'text');
tmpType.setAttribute('name', 'contenttype');
tmpType.setAttribute('value', "my:document");
tmpForm.appendChild(tmpType);
tmpName = document.createElement('input');
tmpName.setAttribute('type', 'text');
tmpName.setAttribute('name', 'filename');
tmpName.setAttribute('value', f.name);
tmpForm.appendChild(tmpName);
tmpData = document.createElement('input');
tmpData.setAttribute('type', 'file');
tmpData.setAttribute('name', 'filedata');
tmpData.setAttribute('value', f);
tmpForm.appendChild(tmpData);
Alfresco.util.Ajax.request({
url: Alfresco.constants.PROXY_URI_RELATIVE + "api/upload",
method: 'POST',
dataForm: tmpForm,
successCallback: {
fn: function(response) {
console.log("SUCCESS!!");
console.dir(response);
},
scope: this
},
failureCallback: {
fn: function(response) {
console.log("FAILED!!");
console.dir(response);
},
scope: this
}
});
}
}
The server responds with a 500, and if I turn on debug level logging for web scripts, upload.post returns with:
DEBUG [repo.jscript.ScriptLogger] ReferenceError: "formdata" is not defined.
Which, to me at least, indicates that the form above isn't getting submitted properly (if at all). When digging through it all with Chrome dev tools, I notice that that request payload looks drastically different from something such as a REST client. The above code results in the request using Content-Type: application/x-www-form-urlencoded whereas using a REST client, or Alfresco Share's standard uploader(s) are using Content-Type: multipart/form-data. If I need to submit the form using multipart/form-data, what is the easiest way to write out the request body (with the boundaries, Content-Disposition's, etc...) to include the file being uploaded?
I ditched the idea of creating a form HTML Element through javascript, and assume that if a browser supports the File API, and the Drag and Drop API, that they will likely also support the XMLHttpRequest2 API. As per HTML5 File Upload to Java Servlet, The above code now reads:
function handleFileSelect(evt) {
var files = evt.target.files || evt.dataTransfer.files,
xhr = new XMLHttpRequest();
dropZone.className = "can-drop";
evt.stopPropagation();
evt.preventDefault();
for (var i=0,f;f=files[i];i++) {
formData = new FormData();
formData.append('destination', destination);
formData.append('mandatoryMetadata', window.metadataButton.value);
formData.append('contenttype', "my:document");
formData.append('filename', f.name);
formData.append('filedata', f);
formData.append('overwrite', false);
xhr.open("POST", Alfresco.constants.PROXY_URI_RELATIVE + "api/upload");
xhr.send(formData);
}
}
with the necessary event listeners to be added later. It would seem that the Alfresco AJAX methods that come stock and standard heavily modify the underlying requests being made, making it very difficult for one to simply send a FormData() object.
I am trying to upload and save an image file to a server using an XMLHttpRequest POST after allowing file selection to be done on the client's side using HTML5 and java script (using an html input element).
My problem is that cannot find out how to actually get a hold of the file from the server side and save it to the server.
This is my code:
xhr = new XMLHttpRequest();
// Update progress bar etc
xhr.upload.addEventListener("progress", function(evt) {
if (evt.lengthComputable) {
progressBar.style.width = (evt.loaded / evt.total) * 100 + "%";
}
else {
// No data to calculate on
}
}, false);
// File uploaded
xhr.addEventListener("load", function() {
progressBarContainer.className += " uploaded";
progressBar.innerHTML = "Uploaded!";
}, false);
xhr.open("post", "imageSave.aspx", true);
// Set appropriate headers
xhr.setRequestHeader("Content-Type", "multipart/form-data");
xhr.setRequestHeader("X-File-Name", file.fileName);
xhr.setRequestHeader("X-File-Size", file.fileSize);
xhr.setRequestHeader("X-File-Type", file.type);
// Send the file
xhr.send(file);
You can retrieve the file using Request.InputStream but that won't work unless there is no other data in your XHR entry.