8

I would like to allow users of our site to be able to upload images, mp3's and video from the front-end. Can I do this with the entry form template easily enough?

I was thinking maybe I could use Wistia for the video upload with their widget.

6
  • Maybe this question is of interest for you: "Can I accept file uploads using the Craft Guest Entries plugin?" – carlcs Jun 28 '14 at 10:10
  • This would be for logged-in users, so guessing it might be different? – GlennJ Jun 28 '14 at 10:20
  • 3
    Quote from accepted answer: "Take a look at Uploading Files from a Form." Did you read that doc article? – carlcs Jun 28 '14 at 10:35
  • I didn't, thanks I will give that a go! – GlennJ Jun 30 '14 at 13:52
  • @ChristianSeelbach I don’t agree that this post is a duplicate of the Guest Entries question, as this one has nothing to do with Guest Entries. Can you post your previous comment as an actual answer here? – Brandon Kelly Jul 8 '14 at 19:52
10

Yes you can do that with an Entry Form in your template.

Have a look at the assets fields article in the docs, there is a small section about uploading files from front-end forms.

3

We had the same problem using ckeditor for our WYSIWYG. There was nothing to upload images from the redactor.

So we created a controller action to handle the upload and save the image file into one of our Craft asset source folder.

Here is the complete code, the payload response is adapted to what ckeditor is expecting but you can use the first part for any generic asset upload.

public function actionImageUpload() {
    $yourInputName = 'upload';
    $yourAssetSrcHandle = 'yourAssetSrcHandle';
    $this->requirePostRequest();

    $uploadedFile = UploadedFile::getInstanceByName($yourInputName);
    $assetSources = craft()->assetSources->getPublicSources('handle');
    $assetSource = $assetSources[$yourAssetSrcHandle];
    $assetFolder = craft()->assets->getRootFolderBySourceId($assetSource->id);

    $response = craft()->assets->insertFileByLocalPath(
      $uploadedFile->tempName,
      $uploadedFile->name,
      $assetFolder->id,
      AssetConflictResolution::KeepBoth
    );

    if ($response->isSuccess())
    {
      $fileId = $response->getDataItem('fileId');
      $file = craft()->assets->getFileById($fileId);

      $this->returnJson([
        'uploaded' => 1,
        'fileName' => $file->filename,
        'url' => $file->getUrl(),
        'error' => null,
      ]);

    } else {
      $this->returnJson([
        'uploaded' => 0,
        'error' => [
          'message' => 'Upload failed'
        ],
      ]);
    }
  }

In case you are using this for a WYSIWYG look at (and update) your HTML purifier config if the image tag is stripped from the field when saving it.

Hope that helps saving someone's time ;) Cheers

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.