Automatic file upload

Asked

Viewed 1,097 times

-4

I need to make sure that when updating the page, my element <input type="file"> already come loaded with the files that will be saved in the database, remembering that the files will have the same name.

  • If you can explain better the problem you have in particular and, if possible, show code you have done where this problem is. Your question is too broad, see Help Center How To Ask..

  • This question is being discussed at the goal: http://meta.pt.stackoverflow.com/q/1630/7210

  • Can you explain the problem better, set an example with a code snippet or something? I could not understand what the problem is, just to understand what has to do with uploading. But want to upload what files? show the uploads you’ve already saved in the bank etc....

  • If what you need is for upload you can only upload links indicating what has already been uploaded and using the input file only to upload new files. There is one component that may be useful in the case of upload http://www.dropzonejs.com/, can be configured to display downloads that have been successfully made in addition to several other features. However ideal would even edit your question to make your need clearer.

3 answers

1

Do you want the form to be self submitted? If so:

<form id="formulario" method="post" enctype="multipart/form-data">
    <input type="file" onchange="formulario.sumbit()" name="arquivos[]" multiple/>
</form>

The magic is in onchange="formulario.submit()". It executes the function when the value of the input is amended.

The attribute name with [] at the end serve for ease in processing the data in the backend.

The attribute multiple serves to tell the browser to accept multiple files.

0

The input type=file field cannot be loaded again with the files that are in the database, you should display the files otherwise. the file type field is not like a text type field that you can add the text in a value to display to the user.

For example: You uploaded an image in the input file, sent it to the database. In your database you saved the image path that was uploaded to the server. At the time of editing this image you do not upload its data in your input file, you display it in an input file field is empty.

0

Event forms and uploads (single or multiple) are a usability problem.

Due to security restrictions, you cannot set the file name to be sent programmatically. So, if it is necessary to update the form without Ajax, the file will be sent or "lost".

So, what I usually do is untie sending attachments from the rest of the form:

  1. The first step is to automatically upload when the user selects a file. The file is saved in a temporary location and linked to the user’s session with some identifier related to the current form.
  2. After sending, I update the form showing an item with the file name and an option to remove it. The file(s) (s) already sent should be listed through the items saved as described in the previous item.
  3. When the form is effectively submitted I check if there are one or more "temporary" files, including your information in some template table and moving the files to the definitive directory.

The directory with temporary files should be cleaned from time to time, excluding files from abandoned sessions. You can use some criteria related to the date of the file for this, for example, deleting files older than 1 day.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.