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();
}
4
  • Not sure if it helps, but try putting a HttpPostedFileBase upload argument to your UploadSWF and see if it's not null. Commented Feb 18, 2015 at 0: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 <form>. With my original ASP.NET MVC 5 project it was working well. So I tried to find something else for my vNext project... Commented Feb 18, 2015 at 0:54
  • Perhaps you should add your posting code, too. Commented Feb 18, 2015 at 0: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 <form> element. Commented Nov 25, 2021 at 21:46

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

7 Comments

I can only see the Beta2 on Nuget nuget.org/packages/Microsoft.AspNet.Http/1.0.0-beta2 How can I get this Beta3?
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
Can't get this working properly with the Beta3. I think it's too soon. Do you know how to deal with the XmlHttpRequest?
I can see that you are using the name "upload" for the file. Are you using the same name in your action?
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...
|

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.