栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

文件上传ASP.NET MVC 3.0

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

文件上传ASP.NET MVC 3.0

您不使用文件输入控件。在ASP.NET
MVC中不使用服务器端控件。请查看以下博客文章,该文章说明了如何在ASP.NET MVC中实现此目的。

因此,您将从创建一个包含文件输入的HTML表单开始:

@using (Html.BeginForm("Index", "Home", FormMethod.Post, new { enctype = "multipart/form-data" })){    <input type="file" name="file" />    <input type="submit" value="OK" />}

然后您将有一个控制器来处理上传:

public class HomeController : Controller{    // This action renders the form    public ActionResult Index()    {        return View();    }    // This action handles the form POST and the upload    [HttpPost]    public ActionResult Index(HttpPostedFilebase file)    {        // Verify that the user selected a file        if (file != null && file.ContentLength > 0)         { // extract only the filename var fileName = Path.GetFileName(file.FileName); // store the file inside ~/App_Data/uploads folder var path = Path.Combine(Server.MapPath("~/App_Data/uploads"), fileName); file.SaveAs(path);        }        // redirect back to the index action to show the form once again        return RedirectToAction("Index"); }}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/442830.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号