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

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

<p>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.</p>

<pre><code>public function actionImageUpload() {
    $yourInputName = 'upload';
    $yourAssetSrcHandle = 'yourAssetSrcHandle';
    $this-&gt;requirePostRequest();

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

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

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

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

    } else {
      $this-&gt;returnJson([
        'uploaded' =&gt; 0,
        'error' =&gt; [
          'message' =&gt; 'Upload failed'
        ],
      ]);
    }
  }
</code></pre>

<p>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.</p>

<p>Hope that helps saving someone's time ;)
Cheers</p>

CC BY-SA 3.0
2018-03-22T19:20:34.913Z
2018-03-22T19:20:34.913Z
Thomas Joeisseint
<p>Yes you can do that with an <a href="https://craftcms.com/docs/templating/entry-form" rel="noreferrer">Entry Form</a> in your template.</p>

<p>Have a look at the <a href="https://craftcms.com/docs/assets-fields" rel="noreferrer">assets fields</a> article in the docs, there is a small section about <a href="https://craftcms.com/docs/assets-fields#uploading-files-from-front-end-entry-forms" rel="noreferrer">uploading files from front-end forms</a>.</p>

CC BY-SA 3.0
2014-07-09T17:42:19.850Z
2017-01-12T05:06:07.647Z
2017-01-12T05:06:07.647Z
carlcs
<p>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?</p>

<p>I was thinking maybe I could use Wistia for the video upload with their widget. </p>

CC BY-SA 3.0
2014-06-28T10:10:23.370Z
Maybe this question is of interest for you: ["Can I accept file uploads using the Craft Guest Entries plugin?"](http://craftcms.stackexchange.com/q/340/125)
carlcs
CC BY-SA 3.0
2014-06-28T10:20:10.220Z
This would be for logged-in users, so guessing it might be different?
GlennJ
CC BY-SA 3.0
2014-06-28T10:35:45.460Z
Quote from accepted answer: "Take a look at [Uploading Files from a Form](http://buildwithcraft.com/docs/assets-fields#uploading-files-from-front-end-entry-forms)." Did you read that doc article?
carlcs
CC BY-SA 3.0
2014-07-08T19:52:16.413Z
@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
CC BY-SA 3.0
2014-07-09T17:42:54.383Z
@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
<entries><assets>
Frontend upload for images, mp3s and video?
# Frontend upload for images, mp3s and video?

(Asked by: username_0 on 2014-06-28)

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.

**Tags:** entries, assets

> **Comments:**
> **username_1**: Maybe this question is of interest for you: ["Can I accept file uploads using the Craft Guest Entries plugin?"](http://craftcms.stackexchange.com/q/340/125)  
> **username_0**: This would be for logged-in users, so guessing it might be different?  
> **username_1**: Quote from accepted answer: "Take a look at [Uploading Files from a Form](http://buildwithcraft.com/docs/assets-fields#uploading-files-from-front-end-entry-forms)." Did you read that doc article?  
> **username_2**: @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?  
> **username_1**: @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.  

---

## 2 answers

### Answer by username_1 on 2014-07-09. Score: 10 | Accepted answer

Yes you can do that with an [Entry Form](https://craftcms.com/docs/templating/entry-form) in your template.

Have a look at the [assets fields](https://craftcms.com/docs/assets-fields) article in the docs, there is a small section about [uploading files from front-end forms](https://craftcms.com/docs/assets-fields#uploading-files-from-front-end-entry-forms).


---

### Answer by username_3 on 2018-03-22. Score: 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


---

