<p>In short, you have the right idea, but you're not uploading any files with that JavaScript. All you're doing is sending a file id to a Craft action that expects an uploaded file as well.</p>

<p>You can find an insight on how to change your javascript to include file data as well <a href="https://stackoverflow.com/a/10899796/2040791">here</a></p>

CC BY-SA 3.0
2016-11-29T22:26:21.460Z
Thanks for pointing me in the direction I need to go. I've been tinkering with this all day now and haven't seemed to get any closer to a working model. I've updated my code above to reflect where I'm at with it now. 

Correct me if I'm wrong, but it does seem like the replaceFile action does also perform the file uploading. So like you said, the issue seems to still be my javascript not passing along what it needs to. In the console, my request shows [object Object] for the 'file' after it gets passed to the replaceFile action (maybe irrelevant).
Noah Yamen
CC BY-SA 3.0
2016-11-30T10:07:26.913Z
You shouldn't be passing a jQuery object but an HTML node object. Try $("[name=replaceFile-"+fileId+"]")[0] instead of $("[name=replaceFile-"+fileId+"]").
Andris Sevcenko
CC BY-SA 3.0
2016-11-30T15:26:33.953Z
I've updated it based off of your suggestion and still no bueno. replaceFile just keeps returning "error: upload failed". I've updated my code above as well. I'm just testing this on a fresh install of craft, in the templates it included. You can take a look at it here: http://rhetoric.fyi/news/2016/we-just-installed-craft Obviously you'd have to be logged-in in order for it to work for you. For the record I am logged-in while I'm testing it.
Noah Yamen
CC BY-SA 3.0
2016-12-01T09:26:04.090Z
Okay, so final fix, I promise! It actually should have been $("[name=replaceFile-"+fileId+"]")[0].files[0] so you actually add the file from the input field.  If this works for you, please update the code in the question so that other people looking for the same solution can find it.
Andris Sevcenko
CC BY-SA 3.0
2016-12-01T15:34:05.150Z
That did it, thank you Andris! I've updated the code above to reflect the correct working version. 
Noah Yamen
CC BY-SA 3.0
2016-11-29T07:02:42.797Z
2016-11-29T07:02:42.797Z
2017-05-23T12:38:53.957Z
Andris Sevcenko
<p>UPDATE: We've resolved this one. Here is the working code:</p>

<pre><code>&lt;form method="post" enctype="multipart/form-data" action="" accept-charset="UTF-8"&gt;
  &lt;input type="hidden" name="entryId" value="{{ entry.id }}"&gt;
  &lt;input type="hidden" name="action" value="entries/saveEntry"&gt;
  &lt;input type="hidden" name="sectionId" value="2"&gt;
  &lt;input type="hidden" name="enabled" value="1"&gt;

  &lt;input type="file" 
         name="replaceFile-{{ asset.id }}” 
         class="replace-file" 
         data-id="{{ asset.id }}" /&gt;
</code></pre>

<p>...and...</p>

<pre><code>&lt;script src="http://code.jquery.com/jquery-latest.min.js"&gt;&lt;/script&gt; 
&lt;script&gt;
$(".replace-file").change(function() {
  var url = '/actions/assets/replaceFile',
      fileId = $(this).attr("data-id"),
      file = $("[name=replaceFile-"+fileId+"]")[0].files[0],
      formdata = new FormData();

  formdata.append("replaceFile", file);
  formdata.append("fileId", fileId);

  $("#output").html("Uploading...");

  $.ajax({
    url: url,
    type: 'POST',
    data:  formdata,
    cache: false,
    contentType: false,
    processData: false,
    success: function(data) {
      $("#output").html("Supposedly worked...");
    }
  });
});
&lt;/script&gt;
</code></pre>

<assets><ajax>
Trying to replace an asset via replaceFile action in entry form
# Trying to replace an asset via replaceFile action in entry form

(Asked by: username_0 on 2016-11-28)

UPDATE: We've resolved this one. Here is the working code:

  
  
  
  

  

...and...

**Tags:** assets, ajax

---

## 1 answers

### Answer by username_1 on 2016-11-29. Score: 1 | Accepted answer

In short, you have the right idea, but you're not uploading any files with that JavaScript. All you're doing is sending a file id to a Craft action that expects an uploaded file as well.

You can find an insight on how to change your javascript to include file data as well [here](https://stackoverflow.com/a/10899796/2040791)

> **Comments:**
> **username_0**: Thanks for pointing me in the direction I need to go. I've been tinkering with this all day now and haven't seemed to get any closer to a working model. I've updated my code above to reflect where I'm at with it now.   Correct me if I'm wrong, but it does seem like the replaceFile action does also perform the file uploading. So like you said, the issue seems to still be my javascript not passing along what it needs to. In the console, my request shows [object Object] for the 'file' after it gets passed to the replaceFile action (maybe irrelevant).  
> **username_1**: You shouldn't be passing a jQuery object but an HTML node object. Try $("[name=replaceFile-"+fileId+"]")[0] instead of $("[name=replaceFile-"+fileId+"]").  
> **username_0**: I've updated it based off of your suggestion and still no bueno. replaceFile just keeps returning "error: upload failed". I've updated my code above as well. I'm just testing this on a fresh install of craft, in the templates it included. You can take a look at it here: http://rhetoric.fyi/news/2016/we-just-installed-craft Obviously you'd have to be logged-in in order for it to work for you. For the record I am logged-in while I'm testing it.  
> **username_1**: Okay, so final fix, I promise! It actually should have been $("[name=replaceFile-"+fileId+"]")[0].files[0] so you actually add the file from the input field.  If this works for you, please update the code in the question so that other people looking for the same solution can find it.  
> **username_0**: That did it, thank you Andris! I've updated the code above to reflect the correct working version.   

---

