如何在vNext下的MVC 6中上传文件? [英] How can I upload a file in MVC 6 under vNext?

查看:66
本文介绍了如何在vNext下的MVC 6中上传文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在MVC 5中,我曾经这样做:

In MVC 5 I used to do this:

var context = (HttpContextBase)Request.Properties["MS_HttpContext"];
var file = (HttpPostedFileBase)context.Request.Files[0];

现在这些在vNext下的MVC 6中不可用.如何从请求中获取文件?

Now these are not available in MVC 6 under vNext. How can I get the file(s) from the request?

推荐答案

MVC6中尚未实现FileUpload,此问题状态.

FileUpload isn't implemented in MVC6 yet, see this issue, and related issues such as this one for status.

您可以从JavaScript中发布 XMLHttpRequest 并将其捕获像这样的代码:

You can post an XMLHttpRequest from JavaScript and catch it with something like this code:

public async Task<IActionResult> UploadFile()
{
    Stream bodyStream = Context.Request.Body;

    using(FileStream fileStream = File.Create(string.Format(@"C:\{0}", fileName)))
    {

        await bodyStream.CopyToAsync(fileStream);

    }

  return new HttpStatusCodeResult(200);
}

如果您看到所链接的问题,则它们已关闭.您现在可以使用更普通的方式在MVC6中上传文件.

If you see the issues linked, they have now been closed. You can use a more normal way to upload files in MVC6 now.

这篇关于如何在vNext下的MVC 6中上传文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆