delete file after sending to user - yii2

I try to send a file to the user with this function
Yii::$app->response->sendFile($tmp_filename, 'test.RData');
Now I want the file to be deleted after sending. I know that there's an event handler for send method in yii/web/Response called EVENT_AFTER_SEND
I tried to access this handler with the following code:
Event::on(\yii\web\Response::className(), \yii\web\Response::EVENT_AFTER_SEND, function ($event) {
unlink($event->response->filename);
});
But my problem is
a) I'm not sure if this is the right way
b) how to access the filename inside the event
Any help is appreciated!

I had the same problem this week. The documentation says that we can use the $data parameter to add any variable we want on this callback. Here is a example:
Yii::$app->response->sendFile('/path/of/my/temp/file')->on(\yii\web\Response::EVENT_AFTER_SEND, function($event) {
unlink($event->data);
}, '/path/of/my/temp/file');

If you will send the file immediately, you could
header('....');
echo file_get_contents(path/of/file)
unlink(path/of/file);
You could think to create a temporary file, so the operative system will delete it.

Related

Polymerfire - creating data after an event

I'm using the firebase/polymerfire package from bower bower install firebase/polymerfire
how can i create some data in the database after a method has been triggered?
The document tag looks like it will display and update data. I would like to, when a user signs up, create some data for the user to use.
app.signInAnonymously = function() {
this.error = null;
this.$.auth.signInAnonymously();
// add default data for the user template
};
How can i use the default set() or push methods like the normal SDK?
How can i call this on an event from JavaScript?
When trying to bind a path to my document like
<firebase-document
path="/"
data="{{firebaseData}}">
</firebase-document>
{{firebaseData}}
the data won't display, but I have authentication working.
You can actually use the firebase api directly there since firebase-auth is already including it, but if you want to keep the element-based functionality you could do this:
Add an empty firebase-document to your template
<firebase-document id="mydoc"></firebase-document>
Then call its save function in your element
app.signInAnonymously = function() {
this.error = null;
this.$.auth.signInAnonymously();
// add default data for the user template
//set path to null somewhere to avoid overwriting data, I recommend doing it here since save's path update is lazy
this.$.mydoc.path = null;
//set data to the value to set/push
this.$.mydoc.data = {/*your data*/};
//call save, if the second parameter is falsey it'll call push to the first parameter if it's truthy it'll set the data to firstparam/secondparam
this.$.mydoc.save('path/to/users', userid);
};
As for getting data with firebase-document, check that you actually have data in your database and your security rules, if you still can't see your data then it might be related to this issue, do bear in mind that polymerfire is still in a pre-release state

Any way to trigger an 'onchange' function call when a value is changed by an AJAX value return?

I have several drop down menus with a
<... onchange="callthisfunction(...)"...>
When a user manually changes the value the function is called easily enough, but when I use an ajax call to load saved data back into my page, it does not register the drop down value as being changed, and the function call is not triggered. Is there a way to ensure this gets triggered when the value is changed via ajax?
You can use:
document.getElementById("id").onchange()
to generate an onchange event programmatically or you can even call "callthisfunction()" when your ajax call changes the drop down.
onchange:function(){
//temp()
}
afterAjaxSuccess:function(){
//temp();
}
temp:function(){
//code when dropdown is changed or after a service call is made to trigger this function
}

getTag() method not properly working for google apps script

What i am doing in writing a script that lets the User interact with a data table. Every series that the user chooses creates a button, and then plots the series on a graph. if the user click the button it rooms the series. All there the data is stored in a hidden JSON string. the columns, or series that the user whats to see are stored in an array that i call index, it is also a hidden JSON string) Each button is connected to its own client handler, which has a
.forTargets(the index i was talking about).setTag(to the corresponding column in the data array)
and they are all connected to the same server handler. So when the button is clicked the client handler sets the tag for the index to the series that it is supposed to be removed. Now the server handler will run it get the index by ID and get the Tag. This is were it goes wrong. The tag is always null.
The first thing i tried was to see if my client handler was not working properly. So i set the tag of the index to some number, but the getTag method in the Server handler still returned null.
idk, but maybe Tags are not saved in the UI instance??
Below is the first bit of the Server handler.
function clickServer(e) {
e = e.parameter;
var app = UiApp.getActiveApplication();
var master = JSON.parse(e.dataTable, "reviver");
var index = JSON.parse(e.index, "reviver");
var hidden = app.getElementById("hiddenIndex");
var tag = hidden.getTag();
I think the issue you are meeting is more related to timing : handlers are called simultaneously, this is true for client an server handlers as well, that means that if the client handler changes a hidden tag value this change happens too late for the server handler function to 'see' it. What you should do is create a separate button to trigger the server handler that the user would use after all the other parameters where set.
This very same method is used in the known workaround used to get radioButtons value
Also, why do you use tags on the hidden widget ? you could use it with direct access by setValue() and e.parameter.hiddenName since they are already invisible by nature... ?
Note also that you can set a value in client handlers as long a these values are defined inside the Ui instance (the do Get function) either by constant variables or by another client Handler in the same function, as shown in the before mentioned example with radioButtons... but that's only a detail.
In this context if you need to get a widget value (inside the doGet function) you should of course use getValue() to get the widget value as no e.parameter is available at this time.
I hope I've been clear enough, re-reading this I'm not very sure but.... just ask if something is missing ;-)
The tags values are passed to handlers via parameters. In this post this fact is explained in details.

Coldfuson CFScript "setSQL" method was not found

I've got a Coldfusion component, with a method in it called getColumnNames
This just queries a MySQL table, and returns the columnlist:
remote string function getColumnNames() {
qProcessCars = new Query();
qProcessCars.setDataSource('#APPLICATION.dsn#');
qProcessCars.setSQL('SELECT * FROM sand_cars WHERE 1 LIMIT 1');
qProcessCars = qProcessCars.Execute().getResult();
return qProcessCars.columnlist;
}
If I access this remotely in the browser, with page.cfc?method=getColumnNames, then I get the expected list of columns back.
However, if I try to access this from inside another method within the component, I get an error
remote string function otherFunction() {
...
sColumns = getColumnNames();
...
}
The error dump for the above code returns the message "The setSQL method was not found".
So can anyone help me find out why it works as a remote call, but not when called from another method inside the same component.
Problem may be caused some kind of race conditions. If you make few calls which interfere, at some point qProcessCars may already be query result, so invoking method is not possible.
I would try to make the qProcessCars variable local scoped (var qProcessCars = new Query();
) and/or try to use another variable name for query results.
Next possible step is to enclose the query building/executing code into named lock.
Ah I've answered my own question again. Sorry.
I've used the same name qProcessCars else where in the component, I hadn't put var in front of them.
I don't know WHY that was causing the problem, but it was. Maybe setSQL can only be called once per query object?

How to Use plugin RowEditing to Updating values

I have a Grid panel and RowEditing Plugin. I search how to use events when i click on Update Button.
I can display alert() but i don't know how to retrieve updates datas ?!!
${prefix}grid.on('validateedit',onEdit, this);
function onEdit(e) {
alert('UPDATE');
};
I have just one line on my Gridpanel. I need to retrieve all data to updating it :)!
Thanks :)
In your onEdit function, you receive the edit object (e), and you can access everything you need to from here.
For example, if I want to access the store... I can do so from within the onEdit function like
var store = e.grid.store;