-
Notifications
You must be signed in to change notification settings - Fork 518
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Ajax Uploads #84
Comments
If you have a File instance and not an UploadedFile one you've already uploaded the file on the server, what you should need the UploadableField field for ? |
@ftassi Because I would like to use the bundle to manage the lifecycle of the file. For instance, for the file to be deleted when the entity is destroyed and to use other functionalities like the template helpers. |
Cool, so the AbstractStorage::upload need to do nothing in your case, and that is what that if does (it just skip the upload if the entity doesn't contains an UploadedFile). What am I missing ? |
I mean that if you take care of the upload, you need store a File instance within the Entity. The rest of the bundle's code should work, as long as you bind a valid File. That if is just preventing the upload, which is fine for you, because you've already uploaded the file |
Two things: I would like the bundle to store the file in the location that is defined in my config file: project_image:
upload_destination: %kernel.root_dir%/../web/uploads/project/images I would like the @Vich\UploadableField(
mapping="project_image",
fileNameProperty="image_name"
) |
Just to make it clear, the idea is to have a generic Ajax Uploader (my code) that saves a temporary version of the file. Then, according to what type of entity the File is attached to it will be moved to the location that was specified in the config file ( |
mmm you want to rename the file after is uploaded then, we are not talking about uploading the file, we are talking about moving it (or renaming). This not what this bundle is for. You can probably achieve this reusing some part of the bundle's code, but this will not work out of the box. I think the best way should be to let the bundle manage the upload process, so you'll get what you need for free. I don't see how an ajax upload should be different from a non ajax one. At certain point you'll have an UploadedFile in your controller, what you need to do is bind that UploadedFile instance to the entity and then save the entity. This binding step is usually done by the form (I'm talking about a Symfony's form here), but you didn't really need a form instance, you can do it manually if you need to. Make sense ? |
If I understood correctly, moving the file is what the bundle does anyway for an protected function doUpload(UploadedFile $file, $dir, $name)
{
$uploadDir = $this->getUploadDirectory($dir, $name);
$fileName = basename($name);
return $file->move($uploadDir, $fileName);
}
For an Ajax upload, the is_uploaded_file function returns false and, thus, the public function move($directory, $name = null)
{
if ($this->isValid() && ($this->test || is_uploaded_file($this->getPathname()))) {
return parent::move($directory, $name);
}
throw new FileException(sprintf('The file "%s" has not been uploaded via Http', $this->getPathname()));
} I might be missing something here...
I just made a quick test (FileSystemStorage only), replacing |
I don't think that is the ajax upload the problem, but instead the fact that you're using the upload method too late, not for "uploading" the file but for "moving" an uploaded file. UploadedFile VS File is correct way to check if a file needed to be uploaded or if it just the file reference for an Entity loaded from DB (imho). I don't know how your code is working but I'm pretty sure that you have a controller that handle the actual file upload. That point is were you should have an UploadedFile, and that point is were you should bind it to the Entity in order to trigger the upload mechanism of the bundle. If you need a more specific help with this I'll need you to share your code with me. We can even add some example on how to manage this kind of use case in the doc (I think Ajax upload is a pretty much common requirement). BTW I'm closing this as is nor a bug report or a feature request. If you want to go further we can post here the solution of your problem and then integrate that in the doc somehow. |
Is there finally any documentation on how to implement correctly an Ajax upload with VichUploaderBundle ? |
Not really. I've never used the bundle for ajax upload, if you have some example we can update the doc. By the way, how the file get to the server is really not important, as long as the entity has the UploadedFile instance, the bundle should work as expected. |
I'll try to propose my own solution soon. Would be happy to have your 2 cents on it then... |
👍 share it and we can discuss it |
Hi, can you have a look at my last comment on this issue: #96 (comment) I'm having a problem preventing me from uploading with Ajax. Indeed, If I send a POST request (with AJAX or not) to upload the image without changing any other filed of my entity, it doesn't get uploaded (not saved on server side FS nor updated in the DB). Is that normal behavior ? (if yes, why?) Is there something to do to make it uploading the image even when not changing any other field of my entity ? Thanks in advance... |
I needed to download a file in a command, so the Then I could download the file to Maybe it'd have been cleaner to use the |
@tobiassjosten Symfony provide already such a behaviour. Look at the 6th parameter |
Yes, the
:) But looking at it now, it seems that parameter not only changes the behavior of Still, switching to using |
@zabojad @regularjack have you find any solution to use Ajax upload with VichUploaderBundle?? |
I'm uploading a file through an Ajax Request and I would like to use this bundle to manage it. The problem is that my file is not an
UploadedFile
but aSymfony\Component\HttpFoundation\File\File
.So, basically, I save the file to disk, create an instance of
Symfony\Component\HttpFoundation\File\File
and then attach it to my entity that has the properUploadableField
annotation.I took a look at the code and I saw that the File needs to be an instance of
UploadedFile
, inAbstractStorage.php
:Is there a reason for the bundle to require the file to be an
UploadedFile
instead of a plainFile
? The way I see it, that fact considerably restricts the usefulness of this bundle because it's limited to files uploaded through a Form. I'm guessing I'm not the only one doing Ajax uploads in Symfony2 and that other people might benefit from not requiring the file to be an instance ofUploadedFile
.The text was updated successfully, but these errors were encountered: