上传文件ASP.NET 5 [英] Upload file ASP.NET 5
问题描述
我想上传5 ASP.NET文件,但我已经看到了互联网上的两种方式失败。
随着XMLHtt prequest这里是我的服务器端code:
公共异步任务<&的ActionResult GT; UploadSWF()
{
StreamReader的读者=新的StreamReader(Request.Body);
VAR文本=等待reader.ReadToEndAsync();
返回查看();
}
:我的客户端:
VAR的客户=新XMLHtt prequest();
功能上传()
{
var文件=的document.getElementById(一个UploadFile);
VAR FORMDATA =新FORMDATA();
formData.append(上载,file.files [0]);
client.open(后,/首页/ UploadSWF,真正的);
//client.setRequestHeader(\"Content-Type,多部分/表单数据);
client.setRequestHeader(内容类型,应用程序/ x-冲击波闪光);
client.send(FORMDATA);
}
不过,我可以从这个唯一得到的是:
------ WebKitFormBoundaryX1h5stVbtaNe6nFw
内容处置:表格数据; NAME =上传;文件名=data.swf在
内容类型:应用程序/ x-冲击波闪光
CWS
;
块引用>:这里是code如何得到这样的:
公众的ActionResult UploadSWF()
{
流的BodyStream = Context.Request.Body;
VAR SR =新的StreamReader(的BodyStream);
变种测试= sr.ReadToEnd();
返回查看();
}所以我得到的文件和内容类型而不是它的内容的名称。
这答案 http://stackoverflow.com/a/26445416/1203116 正在复制流到一个文件但文件创建部分不是为我工作,我不知道发生了什么事情,但没有任何反应。所以我试图做同样的一个MemoryStream,我得到了一个空字符串。
所以最后我尝试另一种方式,利用IFormFile这里所示:<一href=\"https://github.com/aspnet/Mvc/blob/437eb93bdec0d9238d672711ebd7bd3097b6537d/test/WebSites/ModelBindingWebSite/Controllers/FileUploadController.cs\" rel=\"nofollow\">https://github.com/aspnet/Mvc/blob/437eb93bdec0d9238d672711ebd7bd3097b6537d/test/WebSites/ModelBindingWebSite/Controllers/FileUploadController.cs
这个接口应该是Microsoft.AspNet.Http我已经加入到我的项目,但我仍然不能够访问它。我看不到任何IFormFile这个命名空间。:我尝试的第一个方法是使用HttpPostedFileBase像我习惯于在ASP.NET MVC 5,但它不是我的vNext项目。我一直有一个MissingMethodException。
在客户端我的code是:&LT;形式的行动=/家/ UploadSWF的方法=POSTENCTYPE =的multipart / form-data的&GT;
&LT;输入类型=文件名称=文件接受=SWF文件,应用程序/ x-冲击波闪光/ *。&GT;
&LT;输入类型=提交名称=提交/&GT;
&LT; /表及GT;在我的控制器:
公众的ActionResult UploadSWF(HttpPostedFileBase文件)
{
返回查看();
}
解决方案
IFormFile
是作为beta3版发布的一部分。你可能有过时的包。检查你的project.json
,并确保您使用beta3版(或更新版本)的软件包。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 http://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(); }
解决方案
IFormFile
was introduced as part of beta3 release. You probably have outdated packages. Check yourproject.json
and make sure you use beta3(or newer) packages.这篇关于上传文件ASP.NET 5的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!