<p>I haven't tested this but from a brief look into how assets are uploaded you might be able to pass a <code>userResponse</code> value through post, it seems the options you can pass are:</p>

<p><code>keepBoth</code>, <code>replace</code>, <code>cancel</code></p>

<p>So I guess you would need to include the field in the form you are using to upload the asset like:</p>

<pre><code>&lt;input type="hidden" name="userResponse" value="keepBoth"&gt;
</code></pre>

<p>As I said I haven't tried this before and i'm assuming you're hitting the <code>UploadFile</code> action on the assets controller...</p>

CC BY-SA 3.0
2015-05-15T14:29:35.163Z
Tried that, but didn't appear to work. Also the controller doesn't have any mention of that post variable.

The expressUpload controller method looks more promising, but I've not been able to get a decent response from it yet.
GlennJ
CC BY-SA 3.0
2015-05-15T14:32:27.943Z
Are you posting this as an entry, so your action is  `entries/saveEntry` ?
Alec Ritson
CC BY-SA 3.0
2015-05-15T14:33:06.640Z
No, sending it as AJAX to /actions/assets/uploadFile
GlennJ
CC BY-SA 3.0
2015-05-15T14:36:51.567Z
Are you able to edit your answer to show the code you are using?
Alec Ritson
CC BY-SA 3.0
2015-05-15T14:40:30.383Z
Done, see above. Getting a 500 error from the expressUpload controller method, so not sure what is wrong there.
GlennJ
CC BY-SA 3.0
2015-05-15T14:48:38.727Z
Having looked harder, your original suggestion should work... I will try some more!
GlennJ
CC BY-SA 3.0
2015-05-15T14:16:49.180Z
2015-05-15T14:16:49.180Z
Alec Ritson
<p>Turns out the original code was working fine and uploading the asset. It just wasn't returning a 'data.result.success' value.</p>

<p>But you still get the fileId... so just check for that instead :)</p>

CC BY-SA 3.0
2015-05-15T15:47:08.653Z
Glad you figured it out :)
Alec Ritson
CC BY-SA 3.0
2015-05-15T15:08:53.130Z
2015-05-15T15:08:53.130Z
GlennJ
<p>I have an upload form working in my templates (on the frontend), however if I try to upload a file with the same name as an existing asset the file isn't uploaded.</p>

<p>I notice instead a prompt message comes back. </p>

<p>1) I'm not sure how to deal with this, I presume some AJAX call is required.</p>

<p>2) I would prefer it if I could send a value which tells Craft to keep both files without prompting.</p>

<p>What would be the best way of handling this?</p>

<p>The code I've tried is below, using jquery fileupload.</p>

<hr>

<pre><code>var url = '/actions/assets/uploadFile',
    uploadButton = $('&lt;button/&gt;')
        .addClass('btn btn-primary')
        .prop('disabled', true)
        .text('Processing...')
        .on('click', function () {
            var $this = $(this),
                data = $this.data();
            $this
                .off('click')
                .text('Abort')
                .on('click', function () {
                    $this.remove();
                    data.abort();
                });
            data.submit().always(function () {
                $this.remove();
            });
        });

$('#images-upload').fileupload({
    url: url,
    dataType: 'json',
    formData: [
        { name: 'folderId', value: 1 }
    ],
    autoUpload: true,
    acceptFileTypes: /(\.|\/)(gif|jpe?g|png)$/i,
    maxFileSize: 7000000, // 7 MB
    // Enable image resizing, except for Android and Opera,
    // which actually support image resizing, but fail to
    // send Blob objects via XHR requests:
    disableImageResize: /Android(?!.*Chrome)|Opera/
        .test(window.navigator.userAgent),
    previewMaxWidth: 100,
    previewMaxHeight: 100,
    previewCrop: true,

})
</code></pre>

<hr>

<p>And also</p>

<hr>

<pre><code>    window.csrfTokenName = "{{ craft.config.get('csrfTokenName') }}";
    window.csrfTokenValue = "{{ craft.request.getCsrfToken }}";


var url = '/actions/assets/expressUpload',
    uploadButton = $('&lt;button/&gt;')
        .addClass('btn btn-primary')
        .prop('disabled', true)
        .text('Processing...')
        .on('click', function () {
            var $this = $(this),
                data = $this.data();
            $this
                .off('click')
                .text('Abort')
                .on('click', function () {
                    $this.remove();
                    data.abort();
                });
            data.submit().always(function () {
                $this.remove();
            });
        });

$('#images-upload').fileupload({
    url: url,
    dataType: 'json',
    formData: [
        { name: 'elementId', value: $('input[name=entryId]').val() }, 
        { name: 'fieldId', value: 8 },
        { name: window.csrfTokenName, value: window.csrfTokenValue }
    ],
    autoUpload: true,
    acceptFileTypes: /(\.|\/)(gif|jpe?g|png)$/i,
    maxFileSize: 7000000, // 7 MB
    // Enable image resizing, except for Android and Opera,
    // which actually support image resizing, but fail to
    // send Blob objects via XHR requests:
    disableImageResize: /Android(?!.*Chrome)|Opera/
        .test(window.navigator.userAgent),
    previewMaxWidth: 100,
    previewMaxHeight: 100,
    previewCrop: true,

})
</code></pre>

<hr>

<templating><assets>
Allow uploading of asset with same name as existing asset
# Allow uploading of asset with same name as existing asset

(Asked by: username_0 on 2015-05-15)

I have an upload form working in my templates (on the frontend), however if I try to upload a file with the same name as an existing asset the file isn't uploaded.
I notice instead a prompt message comes back. 
1) I'm not sure how to deal with this, I presume some AJAX call is required.
2) I would prefer it if I could send a value which tells Craft to keep both files without prompting.
What would be the best way of handling this?
The code I've tried is below, using jquery fileupload.

var url = '/actions/assets/uploadFile',
    uploadButton = $('')
        .addClass('btn btn-primary')
        .prop('disabled', true)
        .text('Processing...')
        .on('click', function () {
            var $this = $(this),
                data = $this.data();
            $this
                .off('click')
                .text('Abort')
                .on('click', function () {
                    $this.remove();
                    data.abort();
                });
            data.submit().always(function () {
                $this.remove();
            });
        });

$('#images-upload').fileupload({
    url: url,
    dataType: 'json',
    formData: [
        { name: 'folderId', value: 1 }
    ],
    autoUpload: true,
    acceptFileTypes: /(\.|\/)(gif|jpe?g|png)$/i,
    maxFileSize: 7000000, // 7 MB
    // Enable image resizing, except for Android and Opera,
    // which actually support image resizing, but fail to
    // send Blob objects via XHR requests:
    disableImageResize: /Android(?!.*Chrome)|Opera/
        .test(window.navigator.userAgent),
    previewMaxWidth: 100,
    previewMaxHeight: 100,
    previewCrop: true,

})


And also

    window.csrfTokenName = "{{ craft.config.get('csrfTokenName') }}";
    window.csrfTokenValue = "{{ craft.request.getCsrfToken }}";


var url = '/actions/assets/expressUpload',
    uploadButton = $('')
        .addClass('btn btn-primary')
        .prop('disabled', true)
        .text('Processing...')
        .on('click', function () {
            var $this = $(this),
                data = $this.data();
            $this
                .off('click')
                .text('Abort')
                .on('click', function () {
                    $this.remove();
                    data.abort();
                });
            data.submit().always(function () {
                $this.remove();
            });
        });

$('#images-upload').fileupload({
    url: url,
    dataType: 'json',
    formData: [
        { name: 'elementId', value: $('input[name=entryId]').val() }, 
        { name: 'fieldId', value: 8 },
        { name: window.csrfTokenName, value: window.csrfTokenValue }
    ],
    autoUpload: true,
    acceptFileTypes: /(\.|\/)(gif|jpe?g|png)$/i,
    maxFileSize: 7000000, // 7 MB
    // Enable image resizing, except for Android and Opera,
    // which actually support image resizing, but fail to
    // send Blob objects via XHR requests:
    disableImageResize: /Android(?!.*Chrome)|Opera/
        .test(window.navigator.userAgent),
    previewMaxWidth: 100,
    previewMaxHeight: 100,
    previewCrop: true,

})

**Tags:** templating, assets


---

## 2 answers

### Answer by username_1 on 2015-05-15. Score: 2

I haven't tested this but from a brief look into how assets are uploaded you might be able to pass a userResponse value through post, it seems the options you can pass are:
keepBoth, replace, cancel
So I guess you would need to include the field in the form you are using to upload the asset like:


As I said I haven't tried this before and i'm assuming you're hitting the UploadFile action on the assets controller...

> **Comments:**
> **username_0**: Tried that, but didn't appear to work. Also the controller doesn't have any mention of that post variable.  The expressUpload controller method looks more promising, but I've not been able to get a decent response from it yet.  
> **username_1**: Are you posting this as an entry, so your action is  `entries/saveEntry` ?  
> **username_0**: No, sending it as AJAX to /actions/assets/uploadFile  
> **username_1**: Are you able to edit your answer to show the code you are using?  
> **username_0**: Done, see above. Getting a 500 error from the expressUpload controller method, so not sure what is wrong there.  
> **username_0**: Having looked harder, your original suggestion should work... I will try some more!  

---

### Answer by username_0 on 2015-05-15. Score: 1

Turns out the original code was working fine and uploading the asset. It just wasn't returning a 'data.result.success' value.

But you still get the fileId... so just check for that instead :)

> **Comments:**
> **username_1**: Glad you figured it out :)  

---

