1

So in my Vue component, I have the following file upload dialog.

<div class="file has-name is-right">
  <label class="file-label">
    <input class="file-input" type="file" v-on:change="HandleFileUpload" accept=".pdf">
    <span class="file-cta">
      <span class="file-icon">
        <i class="icon-file-pdf"></i>
      </span>
      <span class="file-label">
        Choose a file…
      </span>
    </span>
    <span class="file-name" v-text="UploadFileName" style="width: 16em" />
  </label>
</div>

Which is handled by the following method in my methods section:

HandleFileUpload(e) {
  var files = e.target.files || e.dataTransfer.files;
  console.warn(files);
  if(files.length > 0) {
    var uploadFile = files[0];
    this.UploadFileName = uploadFile.name;
    var formData = new FormData();
    formData.append('Foo', this.Foo);
    formData.append('File', uploadFile, this.UploadFileName);
    axios.post(
      '/Foo/Upload',
      data,
      { headers: { 'Content-Type': 'multipart/form-data' } }
    ).then(/* logic to handle callback */);
  } else {
    this.UploadFileName = null;
  }
}

And finally my MVC controller, wherein lies the issue

[HttpPost]
public ActionResult Upload(FormCollection data)
{
  var Foo= data.Get("Foo").Trim();
  var Files = data.Get("File");
  /* Rest of the method */
}

Files in the MVC controller is null, despite being populated with the actual file data in the JS and in the network call when I inspect it with the developer tools. Am I missing something from the FormData interactions with FormCollection?

1 Answer 1

2

Hm, try this approach, I just checked a web app I created that uses ASP.NET and axios for posting and my JS looks pretty much identical to yours, in ASP.NET, I am basically doing this:

HttpContext context = System.Web.HttpContext.Current;
HttpPostedFile postedFile = context.Request.Files.Count > 0
                ? context.Request.Files.Get(0)
                : null;
Sign up to request clarification or add additional context in comments.

1 Comment

This worked. I still don't know why my FormCollection approach wasn't working, but this provided a case where I was able to do what I needed. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.