Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-11-29T22:26:21.460Z
Answers.Text: 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).
Answers.UserDisplayName: Noah Yamen
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-11-30T10:07:26.913Z
Answers.Text: You shouldn't be passing a jQuery object but an HTML node object. Try $("[name=replaceFile-"+fileId+"]")[0] instead of $("[name=replaceFile-"+fileId+"]").
Answers.UserDisplayName: Andris Sevcenko
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-11-30T15:26:33.953Z
Answers.Text: 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.
Answers.UserDisplayName: Noah Yamen
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-12-01T09:26:04.090Z
Answers.Text: 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.
Answers.UserDisplayName: Andris Sevcenko
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-12-01T15:34:05.150Z
Answers.Text: That did it, thank you Andris! I've updated the code above to reflect the correct working version. 
Answers.UserDisplayName: Noah Yamen
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-11-29T07:02:42.797Z
Answers.LastActivityDate: 2016-11-29T07:02:42.797Z
Answers.LastEditDate: 2017-05-23T12:38:53.957Z
Answers.UserDisplayName: Andris Sevcenko
Body: <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>
  
Tags: <assets><ajax>
Title: Trying to replace an asset via replaceFile action in entry form
ThreadText: # 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:
  
  ```
  <form method="post" enctype="multipart/form-data" action="" accept-charset="UTF-8">
    <input type="hidden" name="entryId" value="{{ entry.id }}">
    <input type="hidden" name="action" value="entries/saveEntry">
    <input type="hidden" name="sectionId" value="2">
    <input type="hidden" name="enabled" value="1">
  
    <input type="file" 
           name="replaceFile-{{ asset.id }}” 
           class="replace-file" 
           data-id="{{ asset.id }}" />
  
  ```
  
  ...and...
  
  ```
  <script src="http://code.jquery.com/jquery-latest.min.js"></script> 
  <script>
  $(".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...");
      }
    });
  });
  </script>
  
  ```
  
  **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 username_1! I've updated the code above to reflect the correct working version.   
  
  ---
  
  