[英]Upload file ASP.NET 5
我正在尝试在ASP.NET 5中上传文件,但我在互联网上看到的两种方式都失败了。 使用XMLHttpRequest,这是我的服务器端代码:
public async Task<ActionResult> UploadSWF()
{
StreamReader reader = new StreamReader(Request.Body);
var text = await reader.ReadToEndAsync();
return View();
}
[编辑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);
}
但我唯一能从中得到的是:
------ 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();
}
所以我得到了文件的名称和内容类型,但没有得到它的内容。
这个答案https://stackoverflow.com/a/26445416/1203116正在将流复制到一个文件中,但是文件创建部分对我不起作用,我不知道发生了什么但没有任何反应。 所以我尝试用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>
在我的控制器中:
public ActionResult UploadSWF(HttpPostedFileBase file)
{
return View();
}
IFormFile
是beta3版本的一部分。 你可能已经过时了。 检查您的project.json
并确保使用beta3(或更新版)的软件包。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.