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.

5
  • Maybe this question is of interest for you: "Can I accept file uploads using the Craft Guest Entries plugin?"
    – carlcs
    Commented Jun 28, 2014 at 10:10
  • This would be for logged-in users, so guessing it might be different?
    – GlennJ
    Commented Jun 28, 2014 at 10:20
  • 3
    Quote from accepted answer: "Take a look at Uploading Files from a Form." Did you read that doc article?
    – carlcs
    Commented Jun 28, 2014 at 10:35
  • @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? Commented Jul 8, 2014 at 19:52
  • @brandon I didn't vote to close here, but yeah, it wasn't the best idea to write an actual answer as a comment or just leave a link to that other answer. Sometimes I forget that this is not g+ here.
    – carlcs
    Commented Jul 9, 2014 at 17:42

2 Answers 2

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 and acknowledge you have read our privacy policy.

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