.NET 6 File Upload - Rename File On Upload

.NET 6 File Upload - Rename File On Upload

iqvctiqvct Posts: 45Questions: 7Answers: 0

I'm currently working on setting up a type of file repository using datatables/editor.

When the NEW button is selected, a form pops up with 4 fields.

The first 3 fields are dropdown options. These options are essentially the metadata on the file being uploaded.

Field 1: Company Name (ex. ABC, XYZ)
Field 2: Fiscal Year (ex. 2022, 2023)
Field 3: Category (ex. Invoice, Receipt)

The 4th field is the file to be uploaded.

In all cases, all 4 options must be filled out.

This works well from the front end. The users can see all of the files and all of the other fields as well, use column filtering to specify a subset of files, etc.

On the backend however, when using a generic file manager like Windows File explorer, all of the files are stored in a single folder using NAME____EXT.

Ideally, files would be stored on the server with the naming convention of something like:

CompanyName-FiscalYear-Category-FileName.ext.

This way someone with access to the backend could easily sort through to find necessary files.

In the javascript I can render the display of the filename as such, but obviously this is only changing what we see on the application, not the actual name on the back end.

I realize that the file is actually being uploaded before the form is submit so this may be a bit tricky.

Any suggestions on a way to have the uploaded file have a custom name? Or a rename after the form is submit?

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 61,697Questions: 1Answers: 10,102 Site admin
    Answer ✓

    Hi,

    The problem here is that the file upload is async to the rest of the form. The upload happens as soon as you select the file. That way we have a reference to the file's identifier (usually a primary key value in a database) which can be saved in the host table.

    The async nature of this simplifies some interactions, but complicates others. For example in your case, consider what would happen if someone uploaded the file first and the selected fields 1-3. Or they uploaded the file and realised they'd selected the wrong value in one of those fields.

    What I'd probably do in this case is to use a server-side event (postCreate and postEdit) to create a symbolic link to the file in question (based on the data submitted). It does mean your NAME_EXT file is still present, but if the admin needs to find a file based on a name, they can now do so.

    Regards,
    Allan

  • iqvctiqvct Posts: 45Questions: 7Answers: 0

    That makes perfect sense. I forgot that they could potentially select the file to upload before selecting the other fields.

Sign In or Register to comment.