How to file upload via custom NC app?


I’m developing a CRM app, it’s already creating entities, saving them to the database and all, but I could not find a way to send a file from Vue 2 front-end, receive it in back-end controller and upload to Files / folder in Files.

I’d like to know if there’s any documentation or tutorial for this, or if anyone can help.

Thanks in advance!

Are you using HTML forms or the @nextcloud/axios library to send from Vue directly (REST-based)?

You can register a controller method and use the IRequest interface (doc) to access uploaded files from forms.

If you upload the file as plain binary, you can access the raw data as well (I would have to look that up).

Hi @christianlupus, thanks for your reply!

I’m using @nextcloud/axios. I checked the IRequest doc you sent, that’s for accessing the file right? What about handling the upload / uploading the file to Files?

Thanks again!

Forgive this slightly-off-topic question, but CRMs are an interest of mine and generally quite ambitious undertakings, so I’m curious: what compelled you to build your own rather than modify a FOSS package like SuiteCRM?

Between SuiteCRM and Nextcloud I find nearly everything I need to run my shop, but integration is minimal so if there’s a better or leaner way I’m keen to learn.

Hi @RGaskin!

We have a very specific flow and fields for our entities (Customer, Contact…), so I developed a tailored solution (custom CRM app) which I found to be easier than modifying something like SuiteCRM.

1 Like

I’ve used the IRequest interface (thanks @christianlupus) to print_r the file in the controller, it’s working fine, but what interface / class / method should I use to move the uploaded temp file to a folder in Files or even the Root folder of Files?

Thanks in advance.

I would use the Nextcloud API (see Nextcloud PHP API (master)). You will have to copy the content manually, I suspect. That way, they are registered in the core already.

1 Like

I just checked in the source code. Obviously, you can use $request->put (or whatever verb you use in the request) to request the file content once. You have to store/stream directly.

You can of course also read the content (e.g. using file_get_content), store it in a NC file, and remove the uploaded file (unlink).

I recently cameback to this feature and got it working. Thanks @christianlupus, the IRequest hint guided me to the right path!

I took a different approach, instead of making the file available through the Files app, I uploaded to IAppData directory.

For others interested, I basically used IRequest and getUploadedFile method inside the upload controller method.

The controller sends given file to service, which uses \OCP\Files\IAppData to locate or create a given folder (“customer-attachments” in my case).

Using $folder->newFile('filename...'), $uploadedContent = fopen('tmp_name_goes_here', 'rb') and $file->putContent($uploadedContent) I was able to write the file to NC data directory.

I used the FileService from Deck app as a base to write my own (link here).

If anyone needs more details or help, feel free to contact me!

1 Like