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?
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;