上传文件ASP.NET 5

Jér*_*VEL 6 .net c# asp.net asp.net-core

我正在尝试在ASP.NET 5中上传文件,但我在互联网上看到的两种方式都失败了.使用XMLHttpRequest,这是我的服务器端代码:

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

[编辑1]:我的客户方:

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);
}
Run Code Online (Sandbox Code Playgroud)

但我唯一能从中得到的是:

------ WebKitFormBoundaryX1h5stVbtaNe6nFw Content-Disposition:form-data; NAME = "上传"; filename ="data.swf"内容类型:application/x-shockwave-flash CWS;"

[编辑2]:这是我如何得到这个代码:

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

所以我得到了文件的名称和内容类型,但没有得到它的内容.

这个答案/sf/answers/1851179151/正在将流复制到一个文件中,但文件创建部分对我不起作用,我不知道发生了什么但没有任何反应.所以我尝试用MemoryStream做同样的事情,我得到一个空字符串.

所以最后我尝试另一种方式,使用IFormFile像如下所示:https://github.com/aspnet/Mvc/blob/437eb93bdec0d9238d672711ebd7bd3097b6537d/test/WebSites/ModelBindingWebSite/Controllers/FileUploadController.cs 这个接口应该是Microsoft.AspNet.Http我已添加到我的项目但我仍然无法访问它.我在这个命名空间中看不到任何IFormFile.

[编辑1]:我尝试的第一种方法是使用HttpPostedFileBase,就像我在ASP.NET MVC 5中习惯的那样,但它在我的vNext项目中无效.我总是遇到MissingMethodException.我在客户端的代码是:

<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>
Run Code Online (Sandbox Code Playgroud)

在我的控制器中:

public ActionResult UploadSWF(HttpPostedFileBase file)
{
     return View();
}
Run Code Online (Sandbox Code Playgroud)

小智 8

IFormFile是作为beta3版本的一部分而引入的.你可能已经过时了.检查project.json并确保使用beta3(或更新版)的软件包.