7

I'm trying to upload a file in ASP.NET 5 but the two ways I've seen on internet fail. With XMLHttpRequest here is my server side code:

 public async Task<ActionResult> UploadSWF()
 {
     StreamReader reader = new StreamReader(Request.Body);
     var text = await reader.ReadToEndAsync();
     return View();
 }

[EDIT 1]: And my client side:

var client = new XMLHttpRequest();
function upload() 
{
   var file = document.getElementById("uploadfile");
   var formData = new FormData();
   formData.append("upload", file.files[0]);
   client.open("post", "/Home/UploadSWF", true);
   //client.setRequestHeader("Content-Type", "multipart/form-data");
   client.setRequestHeader("Content-Type", "application/x-shockwave-flash");
   client.send(formData);
}

But the only thing I can get from this is:

------WebKitFormBoundaryX1h5stVbtaNe6nFw Content-Disposition: form-data; name="upload"; filename="data.swf" Content-Type: application/x-shockwave-flash CWS ;"

[EDIT 2]:Here is the code how I get this:

 public ActionResult UploadSWF()
    {
        Stream bodyStream = Context.Request.Body;
        var sr = new StreamReader(bodyStream);
        var test = sr.ReadToEnd();
        return View();
    }

So I get the name of the file and the content-type but not its content.

This answer https://stackoverflow.com/a/26445416/1203116 is copying the stream into a file however the file creation part isn't working for me, I don't know what's going on but nothing happens. So I tried to do the same with a MemoryStream and I got an empty string.

So finally I tried another way, using IFormFile like shown here: https://github.com/aspnet/Mvc/blob/437eb93bdec0d9238d672711ebd7bd3097b6537d/test/WebSites/ModelBindingWebSite/Controllers/FileUploadController.cs This interface should be in Microsoft.AspNet.Http which I've added to my project but I'm still not able to access it. I can't see any IFormFile in this namespace.

[EDIT 1]: The first method I've tried is by using HttpPostedFileBase like I was used to in ASP.NET MVC 5 but it was not working in my vNext project. I always got an MissingMethodException. My code on client side was:

<form action="/home/UploadSWF" method="POST" enctype="multipart/form-data">
        <input type="file" name="file" accept=".swf, application/x-shockwave-flash/*">
        <input type="submit" name="submit" />
    </form>

And in my controller:

public ActionResult UploadSWF(HttpPostedFileBase file)
{
     return View();
}
Community
  • 1
  • 1
Jérôme MEVEL
  • 7,031
  • 6
  • 46
  • 78
  • Not sure if it helps, but try putting a HttpPostedFileBase upload argument to your UploadSWF and see if it's not null. – Mihai Caracostea Feb 18 '15 at 00:50
  • I've ever tried this. Actually it was the first way I wanted to implement but I always got a MissingMethodException. The request can't reach the method controller with the classic
    . With my original ASP.NET MVC 5 project it was working well. So I tried to find something else for my vNext project...
    – Jérôme MEVEL Feb 18 '15 at 00:54
  • Perhaps you should add your posting code, too. – Mihai Caracostea Feb 18 '15 at 00:57
  • I spent many hours trying to figure out why my IFormFile object was "null" in my ASP.Net 5 OnPost() method. No errors, no warnings ... and no file to upload. SOLUTION: I forgot to add `enctype="multipart/form-data"` to the
    element.
    – FoggyDay Nov 25 '21 at 21:46

1 Answers1

8

IFormFile was introduced as part of beta3 release. You probably have outdated packages. Check your project.json and make sure you use beta3(or newer) packages.

Ajay Bhargav
  • 136
  • 4
  • I can only see the Beta2 on Nuget http://www.nuget.org/packages/Microsoft.AspNet.Http/1.0.0-beta2 How can I get this Beta3? – Jérôme MEVEL Feb 18 '15 at 01:12
  • Ha ok I didn't get that just by changing my project.json it will automatically get the newest one even if Nuget doesn't show it. Thank you Ajay – Jérôme MEVEL Feb 18 '15 at 01:16
  • Can't get this working properly with the Beta3. I think it's too soon. Do you know how to deal with the XmlHttpRequest? – Jérôme MEVEL Feb 18 '15 at 01:35
  • I can see that you are using the name "upload" for the file. Are you using the same name in your action? – Ajay Bhargav Feb 18 '15 at 01:48
  • With IFormFile I had an exception so I guess I have to also add Microsoft.AspNet.PipelineCore Beta3 to my project but the result is an error 500. The name "upload" was the name I use with the XmlHttpRequest method. I can get information about the file like its name or length but I totally don't know how to access its content. There is no Request.File or Request.Files I can use... – Jérôme MEVEL Feb 18 '15 at 02:10
  • @Koenigs If you [modify the URL](http://www.nuget.org/packages/Microsoft.AspNet.Http/1.0.0-beta3), you can see that Beta 3 is on NuGet, Microsoft just chose to unlist it for the moment. As for why they did that, I couldn't tell you. – mason Feb 18 '15 at 03:38
  • Yes yes I've tried with Microsoft.AspNet.Http Beta3 and Microsoft.AspNet.PipelineCore Beta2 but I got an exception: "System.TypeLoadException Method 'get_HasFormContentType' in type 'Microsoft.AspNet.PipelineCore.DefaultHttpRequest' from assembly 'Microsoft.AspNet.PipelineCore, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' does not have an implementation" I'll wait it appears in Nuget, probably this Beta3 is buggy for the moment – Jérôme MEVEL Feb 18 '15 at 12:53