Extjs and Spring success handler - excel

I'm building web application with ExtJs and Spring. With ExtJs I'm uploading excel file and with spring I parse it. Everything works fine, although my success handler in ExtJs code doesn't work. I read many examples about that, but I still can't solve this problem. I hope you guys will help me.
This is ExtJs upload file function:
uploadFile: function(value, fld){
var me = this;
var id = this.getTipasCombobox().getValue();
var record = this.getTipasCombobox().getStore().getById(id);
var timeId = this.getLaikotarpiaiCombo().getValue();
var timeRecord = this.getLaikotarpiaiCombo().getStore().getById(timeId);
var fp = this.getUploadBtn().up('form').getForm();
if(fp.isValid()){
fp.submit({
url: Turtas.Properties.getServicePath()+'/save/' + record.data.resource,
waitMsg: 'Failas yra įkeliamas, prašome palaukti...',
success: function(){
console.log("Upload completed");
me.storeSelection(record.data.resource, timeRecord.data.open, timeRecord.data.year, true, me.restrictedPage())
}
})
}
},
And this is my controller function which I use to parse Excel:
#RequestMapping(value="turtas/save/gelezinkeliai", method=RequestMethod.POST)
public String saveGelezinkeliaiFromExcel(HttpServletResponse response, #RequestParam("file") MultipartFile file){
if (file.getSize() != 0){
ReadExcelFileToList excelFile = new ReadExcelFileToList();
List<Gelezinkeliai> gelezinkeliai = new ArrayList<Gelezinkeliai>();
gelezinkeliai = excelFile.readExcelData(/*fileName*/ file);
gelezinkeliai.remove(0);
Laikotarpis laikotarpis = new Laikotarpis();
laikotarpis = laikotarpiaiService.findById(2014);
for (GelezinkeliaiRest delGelezinkelis : service.getItems(2014, true, false)){
service.delete(delGelezinkelis);
}
List<Gelezinkeliai> finalGelezinkeliai = new ArrayList<Gelezinkeliai>();
for (Gelezinkeliai gelezinkelis : gelezinkeliai){
gelezinkelis.setLaikotarpis(laikotarpis);
finalGelezinkeliai.add(gelezinkelis);
GelezinkeliaiRest gelezinkeliaiRest = new GelezinkeliaiRest();
gelezinkeliaiRest.fromGelezinkeliai(gelezinkelis);
service.save(gelezinkeliaiRest, false);
}
response.setContentType("text/html");
return "{\"success\":true}";
}
else {
return "{\"success\":false}";
}
}
Response content-type is text/html and response is:
{"success":true}
If I manually reload page or store, data is updated, so functions work properly, but i want to catch event when the request ends, to reload store automatically. I have no idea what's wrong. Thanks for responses in advance!

Related

Excel File download through web api. Getting corrupt

I am trying to download a excel file through Web API (using Entity framework). The download is working but I am getting some error dialog about file corrupt when trying to open the file.
Web API code as below:
public HttpResponseMessage GetValue(int ID, string name)
{
MemoryStream stream;
try {
using (DataContext db = new DataContext()) {
dynamic fileObj = (from c in db.FileList c.ID == IDc).ToList();
stream = new MemoryStream(fileObj(0).File);
HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK);
result.Content = new StreamContent(stream);
result.Content.Headers.ContentType = new MediaTypeHeaderValue(fileObj(0).FileContentType);
result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment") { FileName = name };
return result;
}
} catch (Exception ex) {
return Request.CreateResponse(HttpStatusCode.InternalServerError);
}
}
It opens the file with two error dialog and following message.
Excel completed file level validation and repair. Some parts of this workbook may have been repaired or discarded
Trying to solve the same. I compared 2 epplus versions: 4.5.3.3 against 5.2.1. The latter one included a code for closing the stream in the GetAsByteArray procedure. So, I just added those lines to the 4.5.3.3 version and it worked like a charm. Looks like the stream initially included some garbage bites which must be deleted before pumping the file data into that stream.
Tested with the NetCore 3.1 web application. Hope it will solve the issue in your case.
if (save)
{
Workbook.Save();
_package.Close();
/* start of added code */
if (_stream is MemoryStream && _stream.Length > 0)
{
CloseStream();
}
/* end of added code */
_package.Save(_stream);
}
I had the same issue, problem is not in the web api code, but it is in the client side code. For me i was using jquery. Following code fixed it for me.
I was creating a blob from the result, which is not required as, result is already a blob.
window.URL.createObjectURL(result);
Note that i am creating object straight away from the result. Full Jquery code below.
Credit goes to mgracs in here
$.ajax({
type: 'POST',
url: url + "/download",
data: data,
xhr: function () {
var xhr = new XMLHttpRequest();
xhr.responseType = 'blob'
return xhr;
},
success: function (result, status, xhr) {
var filename = "";
var disposition = xhr.getResponseHeader('Content-Disposition');
if (disposition && disposition.indexOf('attachment') !== -1) {
var filenameRegex = /filename[^;=\n]*=((['"]).*?\2|[^;\n]*)/;
var matches = filenameRegex.exec(disposition);
if (matches != null && matches[1]) filename = matches[1].replace(/['"]/g, '');
}
var link = document.createElement('a');
link.href = window.URL.createObjectURL(result);
link.download = filename;
link.click();
}, error: function (a, b) {
console.log('Error');
}
});

SP.Ribbon.WebPartComponent.getWebPartAdder() returns undefined

I am using the source at http://blog.symprogress.com/2010/11/ribbon-insert-any-web-part-using-javascript/ to handle user web part button click event.
The function 'addWebPart()' calls a function 'SP.Ribbon.WebPartComponent.getWebPartAdder()' which is supposed to return adder instance but sometimes it returns undefined.
If I add a while loop to wait for the instance value to return correctly, the browser in my VM stalls for some time. When an instance is returned, the browser becomes responsive again. This only happens in some instances.
I am using SharePoint 2013 and the section of code I am referring to is:
addWebPart = function (wpCategory, wpTitle) {
var webPartAdder = SP.Ribbon.WebPartComponent.getWebPartAdder();
while (webPartAdder == undefined)
webPartAdder = SP.Ribbon.WebPartComponent.getWebPartAdder();
// ... Other stuff ...
}
How can this issue be resolved?
For anyone looking for an answer to this question, turns out you have to call 'LoadWPAdderOnDemand()' function then wait for the event '_spEventWebPartAdderReady'. Then query for 'window.WPAdder':
addWebPartDelayed = function (webPartAdder, wpCategory, wpTitle) {
var webPart = findWebPart(webPartAdder, wpCategory, wpTitle);
if (webPart) {
var zone = WPAdder._zones[0];
var wpid = WPAdder._createWebpartPlaceholderInRte();
WPAdder.addItemToPageByItemIdAndZoneId(webPart.id, zone.id, 0, wpid);
}
else
alert('ERROR: Web part not found! Please try again after sometime.');
},
addWebPart = function (wpCategory, wpTitle) {
var webPartAdder = window.WPAdder;
if (webPartAdder == undefined) {
LoadWPAdderOnDemand();
ExecuteOrDelayUntilEventNotified(
function () {
var webPartAdder = window.WPAdder;
addWebPartDelayed(webPartAdder, wpCategory, wpTitle);
},
"_spEventWebPartAdderReady");
}
else
addWebPartDelayed(webPartAdder, wpCategory, wpTitle);
};

Persist data on disk using chrome extension API

I am trying to save some data which should be available even when restart the browser So this data should persist. I am using Chrome Storage Sync API for this. But when I am restarting my browser, I get empty object on using chrome.storage.get.
Here is my sample code:
SW.methods.saveTaskListStore = function() {
chrome.storage.sync.set({
'taskListStore': SW.stores.taskListStore
}, function() {
if (SW.callbacks.watchProcessSuccessCallback) {
SW.callbacks.watchProcessSuccessCallback(SW.messages.INFO_DATA_SAVED);
SW.callbacks.watchProcessSuccessCallback = null;
}
});
};
SW.methods.loadTaskListStore = function() {
SW.stores.loadTaskListStore = [];
chrome.storage.sync.get('taskListStore', function(taskFeed) {
var tasks = taskFeed.tasks;
if (tasks && !tasks.length) {
SW.stores.loadTaskListStore = tasks;
}
});
};
I guess I am using the Wrong API.
If this is not some copy-paste error, you are storing under key taskListStore and trying to get data under key loadTaskListStore.
Besides that, according to the documentation on StorageArea.get(), the result object is an object with items in their key-value mappings. Thus, in your case, you should do:
chrome.storage.sync.get("taskListStore", function(items) {
if (items.taskListStore) {
var tasks = items.taskListStore.tasks;
...

CKEditor: Using dialogDefinition.onShow() throws C.preview not defined

Modifying CKEditor 3.6.2 is not easy, but I tried hard. One problem that is still open is the following:
In config.js we have:
CKEDITOR.on( 'dialogDefinition', function( ev ) {
var dialogName = ev.data.name;
var dialogDefinition = ev.data.definition;
if(dialogName == 'image') {
dialogDefinition.onShow = function () {
var dialog = CKEDITOR.dialog.getCurrent();
var elem = dialog.getContentElement('info','htmlPreview');
elem.getElement().hide();
// and more stuff to do...
};
}
});
After the editor is loaded, and the user has uploaded an image, the following javascript error is thrown:
Error: C.preview is undefined
Source File: wysiwyg-editor/plugins/image/dialogs/image.js?t=B8DJ5M3
Line: 8
dialogDefinition.onShow seems to cause this error, as removing all elements from the code, and only calling onShow brings up the error. Using onLoad does work!
Using onShow on other dialogs is working fine, only the image dialog is not working as it should.
Btw, I asked in the CKEditor forum but nobody answered.
Stumbled across this when I was looking for a resolution. Figured I'd post what I ultimately did to solve it, albeit not 100% ideal.
CKEDITOR.on( 'dialogDefinition', function( ev ) {
var tab, field, name = ev.data.name,
definition = ev.data.definition;
if( name == 'image' )
{
tab = definition.getContents( 'info' );
field = tab.get( 'htmlPreview' );
field.style = 'display: none';
}
});
This makes the preview window available for processing, just hides it from the dialog window.
Use CKEDITOR.tools.setTimeout() like this:
CKEDITOR.on('dialogDefinition', function(ev) {
var dialogName = ev.data.name;
var dialogDefinition = ev.data.definition;
var dialog = dialogDefinition.dialog;
if (dialogName == 'image2') {
dialogDefinition.onShow = CKEDITOR.tools.override(dialogDefinition.onShow, function(original) {
return function() {
original.call(this);
CKEDITOR.tools.setTimeout( function() {
if (dialog.getContentElement('info', 'src').getValue() == '') {
dialog.selectPage('Upload');
}
}, 0);
}
});
}
});

chrome extension connection issues

I have written an extension for google chrome and I have a bug I need a help solving.
what I do is using either a text selection or an input of text search for photos on flickr and then create a results tab.
The extension works most of the times. but sometimes it creates a blank tab with no results and when I repeat the same search it then shows results. I figured that it's something to do with the html files messaging maybe something to do with them communicating. I have to say that I always receive the results from flickr so that the request/responce with flickr works ok. Sometimes the error happens when I play with other tabs or do something on other tabs while waiting for results. can you please help me figure out where's the fault?
the background file:
function searchSelection(info,tab){
var updated;
if(info.selectionText==null){
var value = prompt("Search Flickr", "Type in the value to search");
updated=makeNewString(value);
}
else{
updated=makeNewString(info.selectionText);
}
var resultHtml;
var xhReq = new XMLHttpRequest();
xhReq.open(
"GET",
"http://api.flickr.com/services/rest/?method=flickr.photos.search&text="+updated+
"&api_key=a0a60c4e0ed00af8d70800b0987cae70&content_type=7&sort=relevance&per_page=500",
true);
xhReq.onreadystatechange = function () {
if (xhReq.readyState == 4) {
if (xhReq.status == 200) {
chrome.tabs.executeScript(tab.id, {code:"document.body.style.cursor='auto';"});
var photos = xhReq.responseXML.getElementsByTagName("photo");
if(photos.length==0){
alert("No results found for this selection");
chrome.tabs.executeScript(tab.id, {code:"document.body.style.cursor='auto';"});
return;
}
var myJSPhotos=[];
for(var i=0; i<photos.length; i++){
var data={"id":photos[i].getAttribute("id"),"owner":photos[i].getAttribute("owner"),
"secret":photos[i].getAttribute("secret"),"server":photos[i].getAttribute("server"),
"farm":photos[i].getAttribute("farm"),"title":photos[i].getAttribute("title")};
myJSPhotos[i]=data;
}
chrome.tabs.create({"url":"results.html"},function(thistab){
var port= chrome.tabs.connect(thistab.id);
port.postMessage({photos:myJSPhotos});
});
}
};
};
xhReq.send(null);
chrome.tabs.executeScript(tab.id, {code:"document.body.style.cursor='wait';"});
}
var context="selection";
var id = chrome.contextMenus.create({"title": "search Flickr", "contexts":[context,'page'],"onclick":searchSelection});
results html: has only a reference to the js file res.js
res.js :
chrome.extension.onConnect.addListener(function(port) {
port.onMessage.addListener(function(msg) {
//*****//
var photos=msg.photos;
createPage(photos);
});
});
I have to mention that when the tab is empty if I put alert on the //*****// part it won't
fire.
but when I print out the photos.length at the tab create call back function part it prints out the correct result.
Try to set "run_at":"document_start" option for your res.js in the manifest.
I think callback from chrome.tabs.create is fired right away without waiting for page scripts to be loaded, so you might try something like this instead:
//global vars
var createdTabId = null;
var myJSPhotos = null;
xhReq.onreadystatechange = function () {
//assign myJSPhotos to a global var
chrome.tabs.create({"url":"results.html"},function(thistab){
createdTabId = thistab.id;
});
}
chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) {
if(changeInfo.status == "complete" && tab.id == createdTabId) {
createdTabId = null;
//now page is loaded and content scripts injected
var port = chrome.tabs.connect(tab.id);
port.postMessage({photos:myJSPhotos});
}
});

Resources