bernhard Posted May 20, 2019 Share Posted May 20, 2019 Hello everyone, I'm having an issue with an ajax loaded file upload field in a processmodule. The file upload field itself works as expected when the field is not ajax loaded (see video): The file is stored as value in the inputfield and will be executed after form submission. But when I load the file upload inputfield in a fieldset that is ajax loaded, the behaviour is different: The file gets uploaded instantly and the JSON return is invalid: Does anybody know how I can modify this behaviour? Actually... the instant ajax upload could be even better for my usecase: Upload the CSV, execute it on the backend, reload the grid with new data (without reloading the page which means I don't lose filters, selections etc). Here is the video demonstrating the issue: Link to comment Share on other sites More sharing options...
Autofahrn Posted May 20, 2019 Share Posted May 20, 2019 30 minutes ago, bernhard said: the JSON return is invalid No instant solution, but did you check in dev tools both request and returned data? If the JSON parser complains the response starts with a <, it seems to receive some HTML. Link to comment Share on other sites More sharing options...
LostKobrakai Posted May 20, 2019 Share Posted May 20, 2019 You can look in the dev tools network tab to see the actual result of the ajax call and the error, which is returned. Afaik ajax upload is only supported for pages though, as the page is a piece of data, where the file can be appended to (with a temp. flag). Everywhere else there's nothing you could append the file to for later processing. Link to comment Share on other sites More sharing options...
flydev Posted May 20, 2019 Share Posted May 20, 2019 I don't know if you call an ___executeXYZ() function in RockCRM to get back your answer, but in one of my app, I have to send the answer from my ProcessModule with : header('Content-Type: application/json'); return json_encode($data); to avoid the first HTML char of the <!DOCTYPE html> string being returned. Link to comment Share on other sites More sharing options...
bernhard Posted May 20, 2019 Author Share Posted May 20, 2019 Thank you all for you quick answers! Seems that I was unclear about the problem. The main problem is that the Inputfield behaves different wether it is loaded instantly or via AJAX. When loaded directly, the field does NOT upload the file instantly. When loaded via AJAX, the file IS uploaded instantly and throws an error. I'm not trying to find a quick solution, I'm trying to find a universal solution that works in both scenarios (because I'm developing a module that can be used anywhere and anyhow (as AJAX or non-AJAX). So the main problem is that I don't know WHY the field behaves different and so I don't know HOW I can modify this behaviour! 2 hours ago, Autofahrn said: No instant solution, but did you check in dev tools both request and returned data? If the JSON parser complains the response starts with a <, it seems to receive some HTML. I'm a bit confused about this error. The network entry shows an empty 200 return, but when I set a breakpoint the responseText is some RockGrid markup: So this error might have something to do with RockGrid. But I'm not worried about this error because the upload has to fail since there is no page to upload the file to. What I want to achieve is that the field does NOT upload the file instantly and only submits the file on form submit. Just like the PW module installer does. 2 hours ago, LostKobrakai said: Afaik ajax upload is only supported for pages though, as the page is a piece of data, where the file can be appended to (with a temp. flag). Everywhere else there's nothing you could append the file to for later processing. In general yes, but there is a way to upload files via InputfieldFile (see the module installer for example). You just need to set a destinationPath property and you can then process this file. This works just fine when the field is loaded non-ajax. I hope I could explain it a little better now ? Thx for your help! 1 Link to comment Share on other sites More sharing options...
Autofahrn Posted May 20, 2019 Share Posted May 20, 2019 3 minutes ago, bernhard said: ...is some RockGrid markup maybe requested URL is wrong or does not handle the AJAX query in that case? ...only guessing, lack of knowledge about the inner workings... ? Link to comment Share on other sites More sharing options...
bernhard Posted May 20, 2019 Author Share Posted May 20, 2019 The endpoint for the AJAX request is .../crm/transactions/?InputfieldFileAjax=1 and if I try to open this site in a new tab I get a prompt "does not respond, do you want to wait...?" after some time. But that would be ok for me. Once more: I want the ajax loaded field to behave just like it does when it is non-ajax loaded. 1 hour ago, bernhard said: The main problem is that the Inputfield behaves different wether it is loaded instantly or via AJAX. When loaded directly, the field does NOT upload the file instantly. When loaded via AJAX, the file IS uploaded instantly and throws an error. The Inputfield changes to "Demo.csv" but the file is NOT uploaded. That's how the field should work on ProcessModules. But it does not work like this when the field is AJAX loaded. Then the field behaves like it was on a regular ProcessPageEdit screen. And I don't know why this is the case. I can't find any differences in the markup or in the code. Edit: Another difference is that the AJAX field shows the drag&drop span while the non-ajax field does NOT show it: I've no idea why that is the case.. Link to comment Share on other sites More sharing options...
bernhard Posted May 20, 2019 Author Share Posted May 20, 2019 public function executeDemo() { $form = $this->modules->get('InputfieldForm'); /** @var InputfieldForm $form */ $tmpDir = $this->files->tempDir('upload_csv'); $f = $this->modules->get('InputfieldFile'); $f->attr('id+name', 'upload_csv'); $f->extensions = 'csv'; $f->maxFiles = 1; $f->descriptionRows = 0; $f->overwrite = true; $f->label = 'Upload CSV yyy'; $f->icon = 'download'; $f->destinationPath = $tmpDir; $f->collapsed = Inputfield::collapsedYesAjax; $form->add($f); return $form->render(); } Simple process module to replicate this issue. Comment the $f->collapsed line and it will work, leave it there and it will upload the file instantly (which is not wanted) Link to comment Share on other sites More sharing options...
bernhard Posted May 20, 2019 Author Share Posted May 20, 2019 I found the issue! I'll file a report on github and link it here when I'm done. https://github.com/processwire/processwire-issues/issues/885 6 Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now